[Python-talk] Profile results... Better pack?

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Thu Oct 15 16:23:43 EDT 2009


kent3737 at gmail.com wrote on 10/15/2009 03:52:31 PM:

> On Thu, Oct 15, 2009 at 2:24 PM,  <bruce.labitt at autoliv.com> wrote:
> > kent3737 at gmail.com wrote on 10/15/2009 12:58:47 PM:
> 
> >> Can you show your code? I suggest you post this specific question to
> >> comp.lang.python with a code sample of what you are doing now. Even
> >> better would be a short, standalone program that demonstrates the
> >> timing. That newsgroup loves optimization questions.
> >>
> >
> > I don't see why not.
> > Do you have to sign up to post?
> 
> No, it is actually a Usenet news group. You can post from any
> newsreader or use Google:
> http://groups.google.com/group/comp.lang.python/topics?gvc=1
> 
> > I don't know how to do the random number generation in straight 
python...
> >
> > I think this is a CME (complete minimal example).
> >
> > # ===============================================
> > from numpy import random
> > from struct import pack
> > import time
> >
> > lenfft = 10084200
> > sigma = 0.1
> > stim = random.normal(0., sigma, lenfft) # 10084200 gaussian random 
numbers
> > (doubles)
> >
> > fmt = '!h'+str(stim.size)+'d'  # makes fmt = '!h10084200d'
> > cmd = 4
> >
> > startTime = time.time()
> > packdat = pack( fmt, cmd, *stim )
> > elapsed = time.time() - startTime
> > print 'Time to pack elements ', elapsed
> > # ==============================================

I did post this.  However, I think I have a pretty good answer using numpy 
functions.
Just figured this out.

Here it is:
# ==================================================
from numpy import random
from struct import pack
import time

lenfft = 10084200
sigma = 0.1
stim = random.normal(0., sigma, lenfft) # 10084200 gaussian random numbers 
(doubles)

fmt = '!h'+str(stim.size)+'d'  # makes fmt = '!h10084200d'
cmd = 4

startTime = time.time()
packdat = pack( fmt, cmd, *stim )
elapsed = time.time() - startTime
print 'Time to pack data using struct.pack %.6f sec ' % elapsed

startTime = time.time()
str1 = pack( '!h', cmd )
str2 = stim.byteswap().tostring(order='C')
packdat2 = str1 + str2
elapsed2 = time.time() -startTime

print 'Time to pack data using numpy methods %.6f sec ' % elapsed2
# ==========================================================

> Is your actual input in a numpy array? If so then this is fine. If
> your real input is a Python list then I would just create a list of
> floats. In your post, explain the context a little - that you are
> doing this for transmission over a socket.
> 
> Kent

I'm using numpy for most of my stuff.

I am so impressed with this.  I can go home happy :) 
I just reduced my original simulation time from 15 seconds to 8.9 seconds!

For those who can't run numpy, method 1 runs in 4.2 seconds, method 1 in 
0.45 sec!!!!

Yay numpy!!!

-Bruce


******************************
Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail.  Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
******************************


More information about the Python-talk mailing list