speedup iterators++

1 view
Skip to first unread message

Thomas Spura

unread,
Dec 13, 2009, 6:07:22 PM12/13/09
to shedskin-discuss
Hi,

I ran a little test with dictionaries.

for i in range(1000000):
for value in d.values():
pass

For a dictionary of length 1000.

With the patch 'ppiter-insteat-iterpp.patch' this little program
speeds up about 4-6% (If you take user or real time.).

Just for the record. In python this takes 2m4.3s and unpatched
compiled 54.8s and patched 52.7s.

Don't know, if this also applies anywhere else. It's not nessessary to
convert all ints from i++ into ++i, because this will be optimized by
the compiler anyway. But for iterators this opviously matters.

Thomas

srepmub

unread,
Dec 14, 2009, 8:52:13 AM12/14/09
to shedskin-discuss
thanks!

I knew decrementing can be faster than incrementing for ints (because
then you can compare to 0, which is done as part of the decrement
instruction), but I haven't seen this optimization before.. but while
I'm sure ++it can be faster than it++ (because compilers are stupid
obviously), the strange thing is, using ++it in dict.values makes the
following program about 10% _slower_ here:

d = dict([(x,x) for x in range(1000)])

sum = 0
for i in range(200000):
sum = 0
for value in d.values():
sum += value
print sum

which version of GCC and compiler flags did you use? and what was your
complete test program?

g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1
CCFLAGS=-O3 -s -fomit-frame-pointer -msse2 -Wno-deprecated

mark.

Thomas Spura

unread,
Dec 14, 2009, 11:39:44 AM12/14/09
to shedskin-discuss
The only difference to your programm is, that I wrote down the
dictionary (x,x) for x in range(1000). So this is not done at runtime.
I didn't use the sum, when doing the same like you (with your programm
posted here) I get:

Before:
real 0m12.776s
user 0m12.245s
sys 0m0.041s

After 'sed -i "s|it++|++it|g" lib/builtin.hpp':
real 0m11.340s
user 0m10.894s
sys 0m0.041s

This was the third round running time ./a.out, not directly the first
one.
-> 11% faster

$ gcc --version
gcc (GCC) 4.4.2 20091027 (Red Hat 4.4.2-7)

I always use the default CFLAGS by shedskin. -O2 -pipe -Wno-deprecated

All fedora programms are compiled with:
$ rpm --eval %optflags
-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-
protector --param=ssp-buffer-size=4 -m64 -mtune=generic

Maybe you could add some to the FLAGS file.

Don't know why it's slower on your system... Here it's always faster.
I also have 64bit, and you atm?

Mark Dufour

unread,
Dec 14, 2009, 1:44:54 PM12/14/09
to shedskin...@googlegroups.com
I always use the default CFLAGS by shedskin. -O2 -pipe -Wno-deprecated

I'm afraid this optimization doesn't work so well with better flags (especially -fomit-frame-pointer I think). on my home computer, I get a small speedup using the default flags, but a slowdown again with the mentioned flags.. so it actually inhibits the fastest possible time I can get here.

Maybe you could add some to the FLAGS file.

yes, I should probably put in better defaults here.. :-/


thanks,
mark.
--
"One of my most productive days was throwing away 1000 lines of code" - Ken Thompson
Reply all
Reply to author
Forward
0 new messages