On Aug 13, 5:16 pm, johnmfis...@comcast.net (John Fisher) wrote:
I am working on a framework for data acquisition in Python 2.5, am
trying to get a structure going more like this:
mark start time
start event
event finishes
count time until next interval
start second event...
rather than this:
start event
event finishes
sleep for interval
start second event
Do you see the difference? I get a true fixed interval from the first,
including the time to accomplish the event task(s). In the second case,
the sleep just gets tacked on at the end of the events, not very
deterministic (timing-wise).
So how do I accomplish this in Python with a minimum of labour?
Thanks for any light you can shed on my darkness...
wave_man
In the first example, you know when to start the second interval,
right? In the second case, you are assuming the second task is
ready to run after the sleep interval.
Here's an example that times how long an external program takes,
the os.popen() returns to the Python script when the external
C program completes, so no need for any sleep interval.
Because of the poorly written factor program, some composites
are falsely reported as unfactorable. This example keeps
re-calling the factor!.exe program until all composites are
factored or marked intractable, and the time to execute each
iteration of factor!.exe is printed after each call.
import os
import time
factor_program = 'factor! -d200 '
the_composites =
[['COMPOSITE_FACTOR','508184298003433059930221143303 11033271249313957919046352679206262204589342623811 236647989889145173098650749']]
the_primes = []
the_intractables = []
phase = 1
the_times = []
while the_composites:
print "="*40
print 'Phase',phase
the_comp = the_composites.pop(0)
print the_comp
print
the_times.append(time.time()) # time how long it takes to run
factor!.exe
the_output = os.popen(factor_program+the_comp[1]).readlines()
the_times.append(time.time())
new_factors = [i.split() for i in the_output]
for i in new_factors: print i
print
if len(new_factors) == 1:
# it's prime or intractable
if new_factors[0][0] == 'PRIME_FACTOR':
the_primes.append([new_factors[0][0],long(new_factors[0][1])])
else:
the_intractables.append([new_factors[0][0],long(new_factors[0]
[1])])
new_factors.pop()
while new_factors:
j = new_factors.pop(0)
if j[0] == 'PRIME_FACTOR':
the_primes.append([j[0],long(j[1])])
else:
the_composites.append(j)
print the_times[phase] - the_times[phase-1],'seconds'
phase += 1
print "="*40
print
print 'Factoring complete'
print
the_primes.sort()
the_intractables.sort()
the_primes.extend(the_intractables)
for i in the_primes:
print i[0],i[1]
print
print "="*40
## ========================================
## Phase 1
## ['COMPOSITE_FACTOR',
'5081842980034330599302211433031103327124931395791 90463526792062622045893426238112366479898891451730 98650749']
##
## ['PRIME_FACTOR', '37']
## ['PRIME_FACTOR', '43']
## ['PRIME_FACTOR', '167']
## ['COMPOSITE_FACTOR', '507787751']
## ['PRIME_FACTOR', '69847']
## ['PRIME_FACTOR', '30697']
## ['PRIME_FACTOR', '89017']
## ['PRIME_FACTOR', '3478697']
## ['PRIME_FACTOR', '434593']
## ['PRIME_FACTOR', '49998841']
## ['PRIME_FACTOR', '161610704597143']
## ['PRIME_FACTOR', '14064370273']
## ['COMPOSITE_FACTOR', '963039394703598565337297']
## ['PRIME_FACTOR', '11927295803']
##
## 0.860000133514 seconds
## ========================================
## Phase 2
## ['COMPOSITE_FACTOR', '507787751']
##
## ['PRIME_FACTOR', '29819']
## ['PRIME_FACTOR', '17029']
##
## 0.0780000686646 seconds
## ========================================
## Phase 3
## ['COMPOSITE_FACTOR', '963039394703598565337297']
##
## ['PRIME_FACTOR', '518069464441']
## ['PRIME_FACTOR', '1858900129817']
##
## 0.0469999313354 seconds
## ========================================
##
## Factoring complete
##
## PRIME_FACTOR 37
## PRIME_FACTOR 43
## PRIME_FACTOR 167
## PRIME_FACTOR 17029
## PRIME_FACTOR 29819
## PRIME_FACTOR 30697
## PRIME_FACTOR 69847
## PRIME_FACTOR 89017
## PRIME_FACTOR 434593
## PRIME_FACTOR 3478697
## PRIME_FACTOR 49998841
## PRIME_FACTOR 11927295803
## PRIME_FACTOR 14064370273
## PRIME_FACTOR 518069464441
## PRIME_FACTOR 1858900129817
## PRIME_FACTOR 161610704597143
##
## ========================================