473,406 Members | 2,713 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.

good way to do side effects on lists?

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
Jul 18 '05 #1
7 1159
Danny Shevitz wrote:
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?
--
Michael Hoffman
Jul 18 '05 #2
Michael Hoffman <m.*********************************@example.com > wrote in message news:<ci**********@pegasus.csx.cam.ac.uk>...
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
Jul 18 '05 #3
danny a écrit :
Michael Hoffman <m.*********************************@example.com > wrote in message news:<ci**********@pegasus.csx.cam.ac.uk>...

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,


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
Jul 18 '05 #4
bruno modulix wrote:
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.

Jul 18 '05 #5
Istvan Albert wrote:
now let's turn on psyco:

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


Judging from this post:

http://groups.google.com/groups?hl=e...ing.google.com

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
Jul 18 '05 #6
Eli Stevens (WG.c) wrote:
Judging from this post:

http://groups.google.com/groups?hl=e...ing.google.com

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
Jul 18 '05 #7
Istvan Albert wrote:
bruno modulix wrote:
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 ?
Jul 18 '05 #8

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

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
7
by: zalzon | last post by:
Is it good practice in C++ for a member function to return data or is it better that data is stored in private member variable and printed in the member function? should i be using int...
9
by: bonono | last post by:
Hi, I initially thought that generator/generator expression is cool(sort of like the lazy evaluation in Haskell) until I notice this side effect. >>>a=(x for x in range(2)) >>>list(a) ...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
23
by: Mantorok Redgormor | last post by:
Can emulation of the logical OR be done in standard C to obfuscate its use? So I don't have to use if(a||b) but instead make that even more obfuscated without its use but testing the same condition
9
by: Rouben Rostamian | last post by:
Consider the following illustrative program: #include <stdio.h> double f(double x) { return x*x; } double g(double x)
13
by: Mantorok Redgormor | last post by:
does volatile really inhibit side effects? that is the rules for sequence points and side effects do not apply to volatile objects? -- nethlek
6
by: Senthil | last post by:
Hi, Whenever i read a C++ book or a newsgroup posting, i come across the terms like " before the side effects completes" , "destructor with side effects" etc. What is this side effect mean in C++...
4
by: Academia | last post by:
I get the following watch message: tsSource.Text.ToUpper() This expression causes side effects and will not be evaluated string The Text is &Edit
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.