<s> """<STR_LIT>""" <EOL> import ctypes , time <EOL> libc = ctypes . CDLL ( '<STR_LIT>' ) <EOL> def sleepMicroseconds ( us ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( us * <NUM_LIT> ) <EOL> def delayMicroseconds ( us ) : <EOL> """<STR_LIT>""" <EOL> libc . usleep ( int ( us ) ) <EOL> class Timespec ( ctypes . Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ ( '<STR_LIT>' , ctypes . c_long ) , <EOL> ( '<STR_LIT>' , ctypes . c_long ) ] <EOL> libc . nanosleep . argtypes = [ ctypes . POINTER ( Timespec ) , <EOL> ctypes . POINTER ( Timespec ) ] <EOL> nanosleep_req = Timespec ( ) <EOL> nanosleep_rem = Timespec ( ) <EOL> def nanosleepMicroseconds ( us ) : <EOL> """<STR_LIT>""" <EOL> if ( us >= <NUM_LIT> ) : <EOL> sec = us / <NUM_LIT> <EOL> us %= <NUM_LIT> <EOL> else : sec = <NUM_LIT:0> <EOL> nanosleep_req . tv_sec = sec <EOL> nanosleep_req . tv_nsec = int ( us * <NUM_LIT:1000> ) <EOL> libc . nanosleep ( nanosleep_req , nanosleep_rem ) <EOL> def sleepDelay ( ms ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( ms / <NUM_LIT> ) <EOL> def delay ( ms ) : <EOL> """<STR_LIT>""" <EOL> ms = int ( ms * <NUM_LIT:1000> ) <EOL> libc . usleep ( ms ) <EOL> def betterDelay ( ms ) : <EOL> """<STR_LIT>""" <EOL> if ( ms >= <NUM_LIT:0.1> ) : <EOL> ms -= <NUM_LIT:0.1> <EOL> ms = int ( ms * <NUM_LIT:1000> ) <EOL> libc . usleep ( ms ) <EOL> def test_delayus ( delayus ) : <EOL> """<STR_LIT>""" <EOL> n_tests = [ <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> , <NUM_LIT:100> , <NUM_LIT:10> , <NUM_LIT:2> ] <EOL> tests = [ <NUM_LIT:0> , <NUM_LIT:0.5> , <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> ] <EOL> total_error = <NUM_LIT:0.0> <EOL> time_no_delay = <NUM_LIT:0> <EOL> for i in range ( len ( n_tests ) ) : <EOL> total = <NUM_LIT:0.0> <EOL> error = <NUM_LIT:0.0> <EOL> for j in range ( n_tests [ i ] ) : <EOL> before = time . time ( ) <EOL> t = tests [ i ] <EOL> if ( t != <NUM_LIT:0> ) : <EOL> delayus ( t ) <EOL> total += time . time ( ) - before <EOL> avg = ( total / n_tests [ i ] ) <EOL> avg *= <NUM_LIT> <EOL> avg -= time_no_delay <EOL> if ( t == <NUM_LIT:0> ) : time_no_delay = avg <EOL> error = abs ( avg - t ) <EOL> if ( t ) : <EOL> total_error += error <EOL> print "<STR_LIT>" % ( tests [ i ] , avg , error ) <EOL> print "<STR_LIT>" % ( total_error / len ( n_tests ) ) <EOL> def test_delayms ( delayms ) : <EOL> """<STR_LIT>""" <EOL> n_tests = [ <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> tests = [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT:0.5> , <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT> ] <EOL> total_error = <NUM_LIT:0.0> <EOL> time_no_delay = <NUM_LIT:0> <EOL> for i in range ( len ( n_tests ) ) : <EOL> total = <NUM_LIT:0.0> <EOL> error = <NUM_LIT:0.0> <EOL> for j in range ( n_tests [ i ] ) : <EOL> before = time . time ( ) <EOL> t = tests [ i ] <EOL> if ( t != <NUM_LIT:0> ) : <EOL> delayms ( t ) <EOL> total += time . time ( ) - before <EOL> avg = ( total / n_tests [ i ] ) <EOL> avg *= <NUM_LIT:1000> <EOL> avg -= time_no_delay <EOL> if ( t == <NUM_LIT:0> ) : time_no_delay = avg <EOL> error = abs ( avg - t ) <EOL> if ( t ) : <EOL> total_error += error <EOL> print "<STR_LIT>" % ( tests [ i ] , avg , error ) <EOL> print "<STR_LIT>" % ( total_error / len ( n_tests ) ) <EOL> print "<STR_LIT>" <EOL> test_delayus ( sleepMicroseconds ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' <EOL> print "<STR_LIT>" <EOL> test_delayus ( delayMicroseconds ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' <EOL> print "<STR_LIT>" <EOL> test_delayus ( nanosleepMicroseconds ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' <EOL> print "<STR_LIT>" <EOL> test_delayms ( sleepDelay ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' <EOL> print "<STR_LIT>" <EOL> test_delayms ( delay ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' <EOL> print "<STR_LIT>" <EOL> test_delayms ( betterDelay ) <EOL> print <NUM_LIT:20> * '<STR_LIT:->' </s>
