C some Speed !!
Posted: December 26, 2011 Filed under: Original Article | Tags: c++, comparison, factorization, java, JVM, open, open-source, opensource, python, source, speed 1 CommentMost programmers know that c programs are the next best thing to assembly language programs in terms of Speed , and here is yet another example .
When solving one of the problems at projecteuler.net.I Suddenly came upon the idea to compare the speed of execution of the same algorithm in different languages.
So here is the program in Three Programming languages , C , Java and Python
First in C
#include<stdio.h> long long LargestPrimeFactor(long long number) { long long n = number , i , Largestfactor = 1; for (i = 2; i <= (n / i); i++) { while (n % i == 0) { Largestfactor = i > Largestfactor? i : Largestfactor; n /= i; } } Largestfactor = n > Largestfactor ? n : Largestfactor; return Largestfactor; } int main (){ printf(" %d \n",LargestPrimeFactor(600851475143)); }
Then in Java
class ex3{ public static long LargestPrimeFactor(long number) { long n = number, factor = 1 , i; for (i = 2; i <= n / i; i++) { while (n % i == 0) { factor = i>factor?i:factor; n /= i; } } factor = n>factor?n:factor; return factor; } public static void main(String [] args){ System.out.println(LargestPrimeFactor(600851475143L)); } }
and now in Python
def LargestPrimeFactor(num): n=num factor = 1 i=2 while( i<= (n/i) ): while( n%i == 0 ): if i>factor : factor = i n /= i i += 1 if n > factor: factor = n return factor print LargestPrimeFactor(600851475143)
I computed the times using the time utility in linux .
I expected the c program to win there was no doubt about that , and java was expected to come in second with python last .
$ time ./a.out 6857 real 0m0.003s user 0m0.004s sys 0m0.000s
$ time java ex3 6857 real 0m0.114s user 0m0.084s sys 0m0.028s
$ time python ex3.py 6857 real 0m0.035s user 0m0.028s sys 0m0.004s
From the results its quite visible that c had won over python and python had won over java ,even though java was supposed to be faster than python.
The reason for python winning is that the cost of starting up the JVM is quite high, when compared with the cost of starting the python interpreter.
So lesson learnt If your program is short run it in python when given a choice , but still C is the king of Speed
Ps: This Question on stackoveflow goes over some more details .
FLEX ible Builder !
Posted: March 20, 2011 Filed under: browser, Flash, Techno irrelevency . | Tags: 4.0, action, actionscript, builder, c++, calculator, designing, flash, flex, java, javascript, jquery, MxML, open, opensource, resume, script, sdk, source, Symposium, web Leave a commentI recently attended a 2 Symposiums at some colleges whose name I don’t want to mention, I was a participant in Web Designing Competition in both . In the first one , I really did not plan , I just made a mash up with jQuery , but in the Second one, I put some effort into planning and I designed a really cool website with jQuery UI , I deserved at least a 2nd prize , but my tough luck would have it , I didn’t win anything !
I noticed then winners of both the competitions were students who created something very basic with Flash , My creating was comparatively complex and had more features , but Students who did Flash got real preference.
I set out to Solve this , I Applied to the program that adobe was offering Flex Builder 4 for free to students ,and I got a reply with a free serial Key .
Then I set out to Learn Flash under flex SDK 4.1.
This was a bit more difficult than I Thought , but I Finally managed to Understand MxML and Action Script .
Armed with this Knowledge I made a Simple Calculator (http://j.mp/ewBMmM )
Being a guy who Likes Open Source stuff , I have made the source code available for free.
Just Right Click –> View Source
In view source mode you get to see the source code and also to download a Zip version .
A nice Feature that is easily built with Flex Builder .
I was surprised to Learn How simple Action Script really is .
Action Script ,JavaScript ,Java , C++ … all these languages are like cousins , You Know one , the other is really similar .
MxML on the other hand is pure XML , but its very easy with Autocomplete Enabled.
So the First Step of My Flash Learning Process is officially complete.
Now I can officially add this to my Resume` .
Python ~ On the fly tutorials
Posted: December 20, 2010 Filed under: Uncategorized | Tags: c++, programming, python Leave a commentI just returned from Day 1 of a workshop on Python programming workshop @ REC .
So I have decided to get things going by writing tutorials as I learn the language , so the next post would be on installing python on windows environment .
But before we go into that lets start with what Python is , So Python is an interpreted, general-purpose high-level programming language , That was from Wikipedia , I will explain briefly but for detailed explanation refer Wikipedia,
Interpreted : Its a type of programming language that is not compiled or linked ,Normally languages like c/c++ need to be compiled into machine language and linked with the necessary modules but languages like Python which are interpreted are executed directly from the source code . Another famous example of an interpreted Language is Javascript but remember Javascript is not a programming language its a scripting language , But Python is a programming Language.
High-Level Language : A high level language is nothing but a fancy term for a programming language where you don’t really have to bother about doing dirty work of garbage collection , memory allocation and other boring stuff like that , you can just focus on coding your program , and all this mundane repetitive activities are taken care of by the Compiler , in the case Python it would be done by the Interpreter .
Python is really easy to pick up if you have a good foundation in programming , if not you can still start with Python .
Somehow I have a small prejudice towards block typed and strongly typed languages , Languages like C/C++ , I still like Python though , but I absolutely hate Basic especially Visual Basic ,thats why I always use C# instead of VB .
Learning to code Win32 Native apps
Posted: December 14, 2010 Filed under: How-To, Internet, Techno irrelevency ., Tutorial, Windows | Tags: 32, applications, apps, c++, microsoft, msdn, native, visual, Windows Leave a commentI have been trying to learn how to write native win32 apps for sometime , I don’t know how this happened but I never came across these most obvious Websites that I should have checked out .
Oh well its better late than never as they say .
http://msdn.microsoft.com/en-us/ff380143
http://msdn.microsoft.com/en-us/visualc/bb496952.aspx