fork(4) download
  1. # http://es.stackoverflow.com/q/23328/127
  2. import cProfile, pstats, io
  3.  
  4. def ne(x):
  5. if x != 'val':
  6. pass
  7.  
  8. def en(x):
  9. if not x == 'val':
  10. pass
  11.  
  12. def ee(x):
  13. if x == 'val':
  14. pass
  15. else:
  16. pass
  17.  
  18.  
  19. x = 'valor diferente'
  20. pr = cProfile.Profile()
  21. pr.enable()
  22. for i in range(5000000):
  23. ne(x)
  24. en(x)
  25. ee(x)
  26. pr.disable()
  27. s = io.StringIO()
  28. sortby = 'cumulative'
  29. ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
  30. ps.print_stats()
  31. print(s.getvalue())
Success #stdin #stdout 10.1s 10872KB
stdin
Standard input is empty
stdout
         15000001 function calls in 4.659 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  5000000    1.589    0.000    1.589    0.000 ./prog.py:8(en)
  5000000    1.562    0.000    1.562    0.000 ./prog.py:12(ee)
  5000000    1.508    0.000    1.508    0.000 ./prog.py:4(ne)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}