Comparing Nested Loops in Python, Java, and C

The following code in python is very time consuming. (I could not wait for the end of the program, although my friend told me that it took 20 minutes.)

But the equivalent code in Java works after about 8 seconds, and in C - 45 seconds.

I expected Python to be slow, but not so much, and in the case of C, which I expected faster than Java, it was actually slower. Is the JVM using any kind of loopback technology to achieve this speed? Is there a reason Python is so slow?

import time st=time.time() for i in xrange(0,100000): for j in xrange(0,100000): continue; print "Time taken : ",time.time()-st 
+4
source share
6 answers

gcc 4.2 with the -O1 flag or higher optimizes the loop, and the program takes 1 million second to execute. This indicator is not very representative, since it is very far from actual use in the world. You do a nested loop for some reason, and you never leave it empty. Python does not optimize the loop, although I see no technical reason why it could not.

Python is slower than C because it is farther from machine language. xrange is a good abstraction, but it adds a heavy layer of machine code compared to a simple C loop.

Source C:

 int main( void ){ int i, j; for (i=0;i<100000;i++){ for (j=0;j<100000;j++){ continue; } } return 0; } 
+2
source

Your test does not measure anything meaningful.

The performance of a language in the real world has little to do with how quickly it performs a tight cycle.

Honestly, I am intrigued by the fact that C and Java take as much time as they did; I would expect both of their compilers to understand that nothing is happening inside the inner loop, and optimize both of them into oblivion (and 0 seconds).

Python, on the other hand, is still interpreted (I could be wrong about that). In any case, it seems that the outer loop needs to build 100,000 xrange objects on which the empty inner loop starts, and this is unlikely to be optimized.

So, all you really measure is the ability of various compilers to see that no real computing work is being done.

+12
source

Lesson: performance will never be what you expect. Therefore, always measure, never believe.

Some reasons why you can see these numbers (and from the first sentence, some of them may be completely wrong):

C compiled for the "i586" processor (also called Pentium). This processor was sold from 1993 to 2000. Have you seen him recently? I think no. Thus, the C code is not really optimized for what your processor can do (or, conversely, in a different way: today processors are trying very hard to be a fast Pentium processor). Java, OTOH, compiled for your processor when loading code. It can pull out some tricks that C simply cannot. The price is that program C starts at 15 ms, and Java program starts at 4 seconds.

Python does not have JIT (only at compiler time). All code is converted to bytecode, which is then interpreted. This means that the loop above turns into a dozen bytecode instructions, which are then interpreted by C. This takes time. Python is not intended for huge loops, it is for smart algorithms that you simply cannot express in any other language (at least not with the same amount of code and readability).

Thus, just as it makes no sense to go shopping with an 18 t truck (you can transport everything, but you won’t find a parking place), choose your programming language according to the problem you need to solve. Should it be small and fast? C. Just fast? Java. Flexible? Python Flexible & Fast: Python with a helper library in C (e.g. NumPy).

+9
source

Is there any reason for Python to be so slow?

Yes.

But what does it matter? You have created 100,000 xrange objects. What for? What does it matter? What is your real performance question? Which algorithm is actually too slow?


 for i in xrange(0,100000): # Creates one xrange object for j in xrange(0,100000): # Creates a fresh xrange object each time through the loop 
+5
source
 for i in xrange(0, 10000): for j in xrange(0, 10000): pass 

or

 for i in xrange(0, 100000000): pass 

Python 2.6.5 - Time: 8.50866484642

PyPy 1.3 - Time: 1.55999398232

the reason for slow work is not to create xrange objects

+3
source

A good compiler optimizes the loop.

Assuming the loop is not optimized, I would expect Python to be about 100 times slower than the C version

+1
source

Source: https://habr.com/ru/post/1316656/


All Articles