473,287 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

performance question

I need speed here. What will be the fastest method or does it matter?

(for the example 'a' is only 3 values for the clarity of the example)
a = [1,3,4.] ##
method1:

f.write("vec %f %f %f \n" % (a[0],a[1],a[2]))

method2:

f.write("vec " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n")

also it there a relevant speed difference between making few small write
instead of 1 bigger one.

Thanks for any feed back,
Eric
Mar 15 '07 #1
2 1144
Eric Texier <te********@yahoo.comwrote:
I need speed here. What will be the fastest method or does it matter?

(for the example 'a' is only 3 values for the clarity of the example)
a = [1,3,4.] ##
method1:

f.write("vec %f %f %f \n" % (a[0],a[1],a[2]))

method2:

f.write("vec " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n")

also it there a relevant speed difference between making few small write
instead of 1 bigger one.
Learn to use the timeit module from the standard library, particularly
via the handy -mtimeit commandline switch, and you can measure
performance issues for yourself. E.g., on my laptop:

brain:~ alex$ python -mtimeit -s"a=[1,3,4];f=open('/dev/null','w')"
'f.write("vec %f %f %f \n" % (a[0],a[1],a[2]))'
100000 loops, best of 3: 5.64 usec per loop

brain:~ alex$ python -mtimeit -s"a=[1,3,4];f=open('/dev/null','w')"
'f.write("vec " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n")'
100000 loops, best of 3: 3.36 usec per loop

So, the ugly "method 2" is about 2.3 microseconds faster than the nicer
"method 1" -- when the items of a are ints which method 1 widens to
floats while method 2 doesn't (the results are different between the
methods). When they're floats to start with...:

brain:~ alex$ python -mtimeit -s"a=[1.,3.,4.];f=open('/dev/null','w')"
'f.write("vec %f %f %f \n" % (a[0],a[1],a[2]))'
100000 loops, best of 3: 5.45 usec per loop

brain:~ alex$ python -mtimeit -s"a=[1.,3.,4.];f=open('/dev/null','w')"
'f.write("vec " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n")'
100000 loops, best of 3: 6.26 usec per loop

then method 1 accelerates a little bit and method 2 slows down a lot, so
method 1 is actually about 0.8 microseconds faster.

Make sure you do your measurements with data that well represents your
actual application needs, and -mtimeit will serve you well (if you don't
care about that microsecond or two either way, which is often the case,
then choose the nicer metod 1, of course:-).
Alex
Mar 15 '07 #2
[Eric Texier]
I need speed here. What will be the fastest method or does it matter?
Follow Alex's advice and use the timeit module, but do not generalize
from too small examples; otherwise, the relative timings will be
thrown-off by issues like the time to lookup "write" and "a" and "str"
(all of which will be faster if made a local). Likewise, do the
timings with the actual expected vector length.

(for the example 'a' is only 3 values for the clarity of the example)
a = [1,3,4.] ##
f.write("vec %f %f %f \n" % (a[0],a[1],a[2]))
It's a waste of time to lookup and repack with (a[0],a[1],a[2]).
Instead, try:
f.write('vec %f %f %f' % tuple(a))

f.write("vec " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n")
Often, it is better to join than to make successive concatenations:

f.write('vec' + ' '.join(map(str, a)))

also it there a relevant speed difference between making few small write
instead of 1 bigger one.
Yes. One big one is faster than many small.
Raymond

Mar 15 '07 #3

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

Similar topics

7
by: Randell D. | last post by:
Folks, I have a Javascript performance question that I might have problems explaining... In PHP, better performance can be obtained dealing directly with a variable, as opposed to an element...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
6
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.