Sunday, May 17, 2009

Python: Look What the Stork Dragged In

I have this Python program that I haven't run in a couple years. I wanted to see if I could make it run faster. Last time, it took 22 hours. Sure enough, using some advanced optimization techniques (and a swig of castor oil), my wife and I were able to run the program in a mere 6.5 hours! Here it is:
#!/usr/bin/env python

"""Help Gina-Marie time her contractions."""

import time

SECS_PER_MIN = 60


last_start = None
while True:
print "Press enter when the contraction starts.",
raw_input()
start = time.time()
if last_start:
print "It's been %s minutes %s seconds since last contraction." \
% divmod(int(start - last_start), SECS_PER_MIN)
last_start = start
print "Press enter when the contraction stops.",
raw_input()
stop = time.time()
print "Contraction lasted %s seconds." % int(stop - start)
print

31 comments:

Shannon -jj Behrens said...

Jocylenn Grace Behrens was born at home on 5/16/2009 at 11:47PM after 6.5 hours of labor. She weighed 10lbs 8oz. Both her and my wife are recovering nicely, and we're all grateful for their good health.

Noah Gift said...

Wow, congratulations to both of you.

Tarek said...

Congratulations !

>>> def i_wish_you(something):
... print ''.join([chr(i) for i in something])
...
>>> i_wish_you((72, 97, 112, 112, 105, 110, 101, 115, 115))

Buster said...

Congrats! :)

Will said...

Congratulations!
P.S. That's one of the geekiest baby announcements I've ever read :-)

JoeSmurfy76 said...

Congratulations Friend, I'm very happy for both of you. I have been following the updates on Facebook. I miss you both and can't wait to see you all again. Take care. Your buddy and long time friend, Joe Murphy

Eric said...

Congratulations and best wishes to you and yours.

Leon Atkinson said...

Gratz!!!11!1!!!

Leon Atkinson said...
This comment has been removed by the author.
infixum said...

JJ,
Congratulations to you and the lady of the house.
That's great news.
Carl T.

Shannon -jj Behrens said...

Tarek:

[84, 104, 97, 110, 107, 115, 44, 32, 84, 97, 114, 101, 107, 33]

Shannon -jj Behrens said...

Thanks, everyone!

metapundit.net said...

Congratulations JJ!

-Simeon

Steve said...

Many congratulations. Glad mother and daughter are both doing well.

yacitus said...

Congratulations! How many children is that now?

Eddy Mulyono said...

Congrats, JJ.

May God richly bless you and your family for your generosity to the service of life!

Brandon Corfman said...

congratulations!

Shannon -jj Behrens said...

The midwife corrected me. It turns out that she didn't grab the baby by the head. She slipped in her hand and hooked a finger under the baby's armpit. I was wondering how she could yank so hard without hurting the baby's neck ;) I guess those are the tricks you learn after you've delivered a few thousand kids!

krishna2 said...

Hearty wishes and Congratulations!!!

Shannon -jj Behrens said...

> Congratulations! How many children is that now?

5!

Tongue in cheek:
I figure now that I've got the process down, why should I stop? I'm going to tell everyone else to leave child rearing to the experts ;)

> May God richly bless you and your family for your generosity to the service of life!

Thanks, Eddy!

Joe Louderback said...

> 5!

120 kids? Wow :-)

Congratulations to both of you.

Shannon -jj Behrens said...

> 120 kids? Wow :-)

Yeah, but to be fair, some of those were multiples ;)

/me giggles

Kelly Yancey said...

Congrats to you both! Knowing you, I suppose plans for the next release, JJ 6.0, are already in the works. :P

Bob Van Zant said...

Congrats dude! Heather and I are very happy for you guys. And holy crap, that's a big kid. A Behrens for sure. Can't wait to meet her.

Alex said...

'eJxzzs9LL0osKVYoyVeozC9VSMxLUSjJSFXIzQeSRYoK4ZnFGZl56XA5IF2kkJaYm5lTqZCVXwkW\ny0gsKMjMSy0uVlSwcgEAuH0dDw==\n'.decode("base64").decode("zlib")

Shannon -jj Behrens said...

Thanks Bob and Kelly!

Alex--cute ;)

Venkatraman.S said...

Congrates! Will she drink the AntiGravity Cola? :)

Ivan Babichuk said...

JJ, congratulations from Ukraine! Wish you God's Bless and good health for baby, mother and whole family.

Shannon -jj Behrens said...

Thanks, Ivan :)

Shane said...

I wanted to thank you for the program you posted. I have added to it a little for my use and wanted to show you what I have done with it... This is being run from ipython console with the -pylab option.

import matplotlib.pyplot as plt
import time

spacing = [0]
dur = [0]
count = [0]
counter = 0
spacingholder = 0
SECS_PER_MIN = 60

last_start = None
while True:
print "Press enter when the contraction starts.",
raw_input()
start = time.time()
if last_start:
print "It's been %s minutes %s seconds since last contraction." \
% divmod(int(start - last_start), SECS_PER_MIN)
spacingholder = ((start - last_start)/60.0)
last_start = start
print "Press enter when the contraction stops.",
raw_input()
stop = time.time()
print "Contraction lasted %s seconds." % int(stop - start)
counter = counter + 1
dur.append(int(stop - start))
count.append(counter)
spacing.append(spacingholder)

plt.subplots_adjust(hspace=0.32,top=.93,bottom=.08) #tweak subplots so that labels fit
plt.figure(1)

plt.subplot(211)
plt.plot(count,spacing,"ro")
plt.xlabel("Contraction")
plt.ylabel("Minutes")
plt.title("Time Between Contractions")

plt.subplot(212)
plt.plot(count,dur,"ro")
plt.xlabel("Contraction")
plt.ylabel("Seconds")
plt.title("Duration of Contractions")

Shannon -jj Behrens said...

Hahahaha!!! Congrats!!! Did you do this while she was in labor?