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 .
Professionally git
Posted: August 6, 2011 Filed under: post | Tags: amazon, book, control, git, github, linux, open, open-source, paperback, pro, pro-git, professional, progit, programming, revision, source, System, vcs, version Leave a commentGit (not to be mistaken for the English-slang word) , is a version control system from the maker of the Linux Kernel , who seems to name everything after himself (to be mistaken for the English-slang word ).
git is more than just a version control system , Its light weight , lightning fast Distributed Revision Control System .
I learnt on how to use git the hard way , (i.e) on my own without a book .
But for those of you wanting to gain in-depth knowledge or just start-up with git you can try this free E-book http://progit.org/ebook/progit.pdf and http://progit.org/book/ .
Image Courtesy : Amazon