The code below is pretty generic. In it's final state, it will run some operations (such as sampling data from a serial stream), but my goal right now is to optimize the looping structure to run as fast as possible.
Currently, I am getting a max frequency of ~100-140 before I trip the if statement.
Is there a more efficient way to run this?
NOTE: I know the while loop is essentially empty. Of course the limiting factor of how fast it will run will be the function that I call inside of it. What I'm trying to ensure is that the code in the while loop is running as efficiently as possible
import time
frequency = float(raw_input("enter sampling frequency in Hz: "))
zero_time=time.time()
i=0
try:
while True:
sample_start_time=time.time()
print 'sample',i, 'taken at', sample_start_time-zero_time
i+=1
sample_end_time=time.time()
if 1/frequency-(sample_end_time-sample_start_time)<0:
print "sampling frequency is too large...closing"
break
time.sleep(1/frequency-(sample_end_time-sample_start_time))
except KeyboardInterrupt:
pass