good way to do side effects on lists?

D

Danny Shevitz

Howdy,

A minor stylistic question: Is there a better way to do:

for thing is myList:
myFunc(thing)

map is close, but returns a list, which is wasted. I'm purely using side
effects and the lists are large.

thanks,
Danny
 
M

Michael Hoffman

Danny said:
A minor stylistic question: Is there a better way to do:

for thing is myList:
myFunc(thing)

That seems like the best way to do it to me. It's very clear what it
means and any alternative techniques are of questionable benefit.
map is close, but returns a list, which is wasted. I'm purely using side
effects and the lists are large.

If you really wanted a one-liner

import itertools
class Dummy(object): pass

Dummy() in itertools.imap(myFunc, myList) # exhausts the iterable since your new Dummy instance will never be in the list

But with all that set-up it's not really a one-liner. And what's wrong with
your first proposal anyway?
 
D

danny

Michael Hoffman said:
But with all that set-up it's not really a one-liner. And what's wrong with
your first proposal anyway?

Nothing is really wrong with it. I assume it runs a little slower than
something native, and since it's inside a Monte Carlo code it may
matter a little. Mostly it just feels like there should be a way to do
it better.

Danny
 
B

bruno modulix

danny a écrit :
Nothing is really wrong with it. I assume it runs a little slower than
something native,

Dont assume, test !-)

def fun(arg):
if arg > 500 :
a = 42
else:
b = 84

def withfor(alist):
for item in alist: fun(item)

def withmap(alist):
map(fun, alist)

alist = range(1000)

from timeit import Timer

t1 = Timer("withfor(alist)", "from __main__ import withfor, alist")
t2 = Timer("withmap(alist)", "from __main__ import withmap, alist")

t1.repeat(3, 1000)
[1.9760220050811768, 1.975412130355835, 1.9746799468994141]

t2.repeat(3, 1000)
[2.1670758724212646, 2.1661739349365234, 2.1647889614105225]

HTH
Bruno
 
I

Istvan Albert

bruno said:
Dont assume, test !-)

Here is what I get running the script normally:

[1.4226769044669085, 1.3523785844290266, 1.3683993356697575]
[1.267016338668741, 1.2757789048608128, 1.2636061287118894]

now let's turn on psyco:

import psyco
psyco.full()

......

[0.02277663781290639, 0.02202207263772351, 0.021649958304756606]
[2.6641092652837162, 2.7028650289352423, 2.628167825799089]

oh boy.

Istvan.
 
E

Eli Stevens (WG.c)

Istvan said:
now let's turn on psyco:

import psyco
psyco.full() ....
oh boy.

Judging from this post:

http://groups.google.com/[email protected]

It seems like Psyco can detect simple "hey, let's see how fast this can
go with Psyco" tests, and can give skewed results. I wouldn't expect a
50x improvement in the general case (though if you have to do simplistic
things over and over, Psyco is the way to go ;).

I'm not trying to bash Psyco - two lines netting a 2x speed boost is
great, but it's good to avoid setting expectations too high. :) I
started experimenting with Psyco a few weeks ago, and found I was
getting about 2x for my application. ISTR the docs saying that more
numerical stuff worked well with Psyco; the OP hinted that there was
number crunching involved. Certainly worth a look.

Eli
 
P

Peter Hansen

Eli said:
Judging from this post:

http://groups.google.com/[email protected]

It seems like Psyco can detect simple "hey, let's see how fast this can
go with Psyco" tests, and can give skewed results. I wouldn't expect a
50x improvement in the general case (though if you have to do simplistic
things over and over, Psyco is the way to go ;).

I'm not trying to bash Psyco - two lines netting a 2x speed boost is
great, but it's good to avoid setting expectations too high. :)

That's a good point. I was actually disappointed with Psyco
when I tried it because hyped reports based, I suspect, on
trivial cases had given me high expectations. I believe I
achieved about a 12% speedup...

-Peter
 
B

Bruno Desthuilliers

Istvan said:
bruno said:
Dont assume, test !-)


Here is what I get running the script normally:

[1.4226769044669085, 1.3523785844290266, 1.3683993356697575]
[1.267016338668741, 1.2757789048608128, 1.2636061287118894]

For the record, which Python version ?
 

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

Forum statistics

Threads
474,208
Messages
2,571,082
Members
47,683
Latest member
AustinFairchild

Latest Threads

Top