performance question

E

Eric Texier

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
 
A

Alex Martelli

Eric Texier said:
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
 
R

Raymond Hettinger

[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
 
D

Dennis Lee Bieber

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.
Don't know about speed, but both of your examples presume a fixed
AND known length for the data...

What about? (UNTESTED)

f.write("vec %s\n" % " ".join(["%f" % it for it in a]))

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top