PDA

View Full Version : Python vs C

AkiraBergman
February 10th, 2012, 10:45 AM
I am trying to make a Ulam Spiral which involves looking up two very large arrays (in the order order of many millions) of numbers for the iteration of the elements of a very large matrix. I have sorted the arrays and optimized the searching, but it still takes too long. I am doing this in Python.

How much speed increase can I expect if I use C++ ?

squenson
February 10th, 2012, 11:08 AM
It is difficult to answer your question as the result may vary.

If you mainly use the native Python objects (lists, sets, etc) and their core methods, they are very efficient and the gain in C or C++ will be in the range of 10% max, in my opinion. On the other hand, if you have a lot of lines of code, do repetitive mathematical operations implying casting of numbers, then the performance may be 3 to 10 times better in C or C++.

When dealing with prime numbers, you may consider building a very large list with all the prime numbers within a range and then search this list instead of verifying whether a number is prime with mathematical calculations.

AkiraBergman
February 10th, 2012, 11:30 AM
Thank you for the reply. I have already done the building of the primes into an array for searching. I took the prime list from the web so I don't spend time with these. I have another small array of corner numbers to build the square spiral.

So there may be no big gains with C++. I will build a smaller spiral and do further work on that, using Python. Maybe later I will shift to C++.

sisco311
February 10th, 2012, 03:22 PM
Interesting question. :)

JDShu
February 10th, 2012, 07:41 PM
Out of curiosity, are you using scipy or numpy? If you aren't, perhaps you could try using one of those libraries. Another option might be to run what you have using pypy.

AkiraBergman
February 10th, 2012, 09:08 PM
Thanks, pypy is worth trying. Yes I am using numpy.

I realized that I made a mistake in the use of the arrays. I will try and synchronize the arrays with the matrix, by walking them together only once, since they are sorted. That should increase the speed quite a bit and open the way to a very large matrix, perhaps 50-10M.

I will keep you posted if interested.