473,498 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Python Speed Benchmark

Knowing absolutely zero about benchmarks, I was interested in the posting referencing the paper by
Dr. Cowell-Shah. I was curious how long Python takes just to read and display time.clock(), soI made
the simple test below.

I was amazed at the difference in speed depending on when/how the clock times were converted to
milliseconds....almost 4 to 1...as indicated in the RESULTS below. I understand the variation in the
times is probably due to other running processes.

Any explanation?

The machine used is: P3, 1gHz, 256 meg of ram, Windows 2000, Python 2.3

Also, I did duplicate the intArithmetic (intMaz), using 1,000,000,000, as per the paper, and it
*did* take a long time. I tried a variety of ways to implement the process (while, range, xrange,
etc), and made some improvement, but not much. And of course I did see the "out of memory" message.

Norm

# THE CODE

import time

module_time_start = time.clock()

start_time_1 = time.clock()
stop_time_1 = time.clock()
print 'Test 1: ', (stop_time_1 - start_time_1) * 1000

start_time_2 = time.clock() * 1000
stop_time_2 = time.clock() * 1000
print 'Test 2: ', stop_time_2 - start_time_2

def get_time_1():

start_time_3 = time.clock()
stop_time_3 = time.clock()
duration = (stop_time_3 - start_time_3) * 1000
print 'Test 3: ', duration

def get_time_2():
start_time_4 = time.clock() * 1000
stop_time_4 = time.clock() * 1000
print 'Test 4: ', stop_time_4 - start_time_4

print '\nOK, now loop the methods...\n'

for ix in range(10):
print
get_time_1()
get_time_2()

module_time_stop = time.clock()

print '\nTotal module time: \n', (module_time_stop - module_time_start) * 1000

# THE RESULTS:

Test 1: 0.00363174649292
Test 2: 0.0167619068904

OK, now loop the methods...
Test 3: 0.00810158833036
Test 4: 0.0145269859717

Test 3: 0.006984127871
Test 4: 0.0145269859717

Test 3: 0.00586666741164
Test 4: 0.0145269859717

Test 3: 0.00614603252648
Test 4: 0.0145269859717

Test 3: 0.00614603252648
Test 4: 0.013968255742

Test 3: 0.00586666741164
Test 4: 0.0128507952826

Test 3: 0.00586666741164
Test 4: 0.0136888906272

Test 3: 0.0055873022968
Test 4: 0.0136888906272

Test 3: 0.00642539764132
Test 4: 0.0145269859717

Test 3: 0.0108952394788
Test 4: 0.0145269859717

Total module time:
2.89142893859
Jul 18 '05 #1
1 1908
en*******@ipns.com:
I was curious how long Python takes just to read and display time.clock() ... I was amazed at the difference in speed depending on when/how the clock times were converted to milliseconds....almost 4 to 1...as indicated in the RESULTS below. I understand the variation in the times is probably due to other running processes.

Any explanation?


There are quite a few things here.

First, if you look at the "Test 3:" and "Test 4:" numbers you'll see it's
only
2 times slower, not 4. Let's start with that case, where the tests you make
are in a function. You can see the PVM op codes generated by using
the dis module
def spam(): .... t1 = time.clock()
.... import dis
dis.dis(spam) 2 0 LOAD_GLOBAL 0 (time)
3 LOAD_ATTR 1 (clock)
6 CALL_FUNCTION 0
9 STORE_FAST 0 (t1)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE def spam2(): .... t1 = time.clock() * 1000
.... dis.dis(spam2) 2 0 LOAD_GLOBAL 0 (time)
3 LOAD_ATTR 1 (clock)
6 CALL_FUNCTION 0
9 LOAD_CONST 1 (1000)
12 BINARY_MULTIPLY
13 STORE_FAST 0 (t1)
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
This shows that the "* 1000" is doing two more things; load the
constant "1000" and then do a binary multiply. The obvious
conclusion is that the factor of 2 slowdown comes from this step.
(That actually surprises me. Function call overhead should be
much more than a simple multiply.)

The next is the precision of the clock. The numbers generated
are pretty well quantized. For example, '0.014527' comes up
quite frequently as does '0.005867'. It appears that your clock
has a resolution of about 0.0003, which is 5% of the smaller
numbers or 2% of the bigger ones. If the time slices are just
at the wrong point then you may get a systematic error of up
to about 6% (ie, if the time.clock is done in just under an
integral time quantum while the time.clock()*1000 is just over
a quantum).

To get around that, you should do more tests, and not have
the results of one test strongly correlated with the other.
(You do because get_time_1() is always followed with a
get_time_2().) The easiest way is to use the timeit module,
either on the command-line or interactively, as
import timeit
timeit.Timer("time.clock", "import time").repeat(3, 10000) [0.0063276180834463958, 0.0066243037524600368, 0.0078663607060889262] timeit.Timer("time.clock()", "import time").repeat(3, 10000) [0.054394886949353349, 0.053860182268920198, 0.05341180138486834] timeit.Timer("time.clock()*1000", "import time").repeat(3, 10000) [0.057691115018485561, 0.059368981429486212, 0.058075800674146194]


As you can see, on my box it takes 0.06 microseconds to do
'time.clock', 0.48 microseconds to make the call, and 0.03
microseconds to do the *1000.

Try those on your machine.

Andrew
da***@dalkescientific.com
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
12425
by: dan | last post by:
It would be an understatement to say I love this language. What used to take me all day now takes 3 hours, and I can spend the rest of the time on my bike thinking about the problems from a high...
467
21167
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
7
2052
by: stormslayer | last post by:
Folks: I've been considering a shift to python. I currently use c++builder (borland) or perl. I do computational models / statistics programming, and was interested in python b/c it a. has...
28
2562
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
11
1428
by: Wilk | last post by:
Hi, In the page http://www.python.org/dev/doc/devel/whatsnew/node7.html the last sentence says : The net result of the 2.4 optimizations is that Python 2.4 runs the pystone benchmark around XX%...
1
1340
by: Rafal Zawadzki | last post by:
I was curious of performance new Python 2.4 "Simpler String Substitutions" so I decided to benchmark it. What I get bluszcz@akira:~/python/benchmarks$ python template.py Normal python string...
83
3555
by: Licheng Fang | last post by:
Hi, I'm learning STL and I wrote some simple code to compare the efficiency of python and STL. //C++ #include <iostream> #include <string> #include <vector> #include <set> #include...
4
2319
by: wang frank | last post by:
Hi, While comparing the speed of octave and matlab, I decided to do a similar test for python and matlab. The result shows that python is slower than matlab by a factor of 5. It is not bad since...
13
2304
by: Corey G. | last post by:
If Perl 6 ever does get on its feet and get released, how does it compare to Python 3000? Is Perl 6 more like Java now with Parrot? I just want to make sure that Python is staying competitive. ...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.