473,406 Members | 2,259 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,406 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 1146
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.