if not pythonsbest min(timeit repeatsetup=setupstmt=stmtnumber=numberrepeat=repeat)elsesetup setup replace('\ ' setup join('- "% "line for line in setup split('\ ')for (ispy pythonin pythonscmd '% - timeit - % - % % % (pythonnumberrepeatsetupargspybench _cases py import pybench sys stmts (num,rpt,setup,stmt( """[ * for in range( )]")( """res=[]\nfor in range( )res append( * )")( "def ( ):\ \treturn ""[ (xfor in 'spam ]")( "def ( ):\ \treturn ""res=[]\nfor in 'spam :\ \tres append( ( ))")( " [ ]""for in range(len( )) [ + ")( " [ ]"" = \nwhile len( ):\ \tl[ + \ \ti + ")pybench runner(stmtspythonstracecmdrun this script with the - and - command-line flags to see how command lines are constructed for setup code for instancethe following test specification tuple generates the command line that follows it for --not nice to look atperhapsbut sufficient to pass lines from windows to timeitto be concatenated with line breaks between and inserted into generated timing function with appropriate reindentation( "def ( ):\ \treturn ""res=[]\nfor in 'spam :\ \tres append( ( ))" :\python \python - timeit - - - "def ( ):- "for in 'spam :res append( ( ))return "res=[]in api call modecode strings are passed unchangedbecause there' no need to placate shelland embedded tabs and end-of-line characters suffice experiment on your own to uncover more about python code alternativesspeed you may eventually run into shell limitations for larger sections of code in command-line modebut both our homegrown timer and pybench' timeit-based api call mode support more arbitrary code benchmarks can be great sportbut we'll have to leave future improvements as suggested exercises timing iterations and pythons with timeit