Skip to main content

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

Comments

jjinux 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.
Anonymous 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))
Unknown said…
Congrats! :)
Will Moffat said…
Congratulations!
P.S. That's one of the geekiest baby announcements I've ever read :-)
Unknown 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 Walstad 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.
Carl Trachte said…
JJ,
Congratulations to you and the lady of the house.
That's great news.
Carl T.
jjinux said…
Tarek:

[84, 104, 97, 110, 107, 115, 44, 32, 84, 97, 114, 101, 107, 33]
jjinux 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!
jjinux 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!
BhogiToYogi said…
Hearty wishes and Congratulations!!!
jjinux 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.
jjinux 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.
Unknown said…
'eJxzzs9LL0osKVYoyVeozC9VSMxLUSjJSFXIzQeSRYoK4ZnFGZl56XA5IF2kkJaYm5lTqZCVXwkW\ny0gsKMjMSy0uVlSwcgEAuH0dDw==\n'.decode("base64").decode("zlib")
jjinux said…
Thanks Bob and Kelly!

Alex--cute ;)
Venkat 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.
jjinux 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")
jjinux said…
Hahahaha!!! Congrats!!! Did you do this while she was in labor?

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within Tunes.org , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p