fork download
  1. # your code goes here
  2. import linecache
  3. import datetime
  4.  
  5. print("###### test loop 1 ############")
  6.  
  7. start_1 = datetime.datetime.now()
  8. for i in range(9):
  9. row = linecache.getline('linecache.py', i+1)
  10. print(row)
  11. linecache.clearcache() ## when to clear the cache? Or out of the for-loop?
  12.  
  13. end_1 = datetime.datetime.now()
  14.  
  15. print("###### test loop 2 ############")
  16.  
  17. start_2 = datetime.datetime.now()
  18. for i in range(9):
  19. row = linecache.getline('linecache.py', i+1)
  20. print(row)
  21. linecache.clearcache() ## when to clear the cache? Or out of the for-loop?
  22. end_2 = datetime.datetime.now()
  23.  
  24.  
  25. # i want that it saves RAM and runs fast
  26.  
  27. ## compare
  28. print("### timing clear cache in loop in microseconds")
  29. delta_1 = end_1 - start_1
  30. print(delta_1.microseconds)
  31.  
  32. print("### timing clear cache out of loop in microseconds")
  33. delta_2 = end_2 - start_2
  34. print(delta_2.microseconds)
  35.  
Success #stdin #stdout 0.01s 9024KB
stdin
Standard input is empty
stdout
###### test loop 1 ############
"""Cache lines from files.



This is intended to read lines from modules imported -- hence if a filename

is not found, it will look down the module search path for a file by

that name.

"""



import sys

import os

###### test loop 2 ############
"""Cache lines from files.



This is intended to read lines from modules imported -- hence if a filename

is not found, it will look down the module search path for a file by

that name.

"""



import sys

import os

### timing clear cache in loop in microseconds
336
### timing clear cache out of loop in microseconds
41