I wrote the following script
Basically, I’m just learning Python for Machine Learning and wanted to check how really computationally intensive tasks would perform. I observe that for 10**8 iterations, Python takes up a lot of RAM (around 3.8 GB) and also a lot of CPU time (just froze my system)
I want to know if there is any way to limit the time/memory consumption either through code or some global settings
Script –
initial_start = time.clock()
for i in range(9):
start = time.clock()
for j in range(10**i):
pass
stop = time.clock()
print 'Looping exp(',i,') times takes', stop - start, 'seconds'
final_stop = time.clock()
print 'Overall program time is',final_stop - initial_start,'seconds'
look at this question: How to limit the heap size?
To address your script, the timeit module measures the time it takes to perform an action more accurately
Your example is taking most of its time dealing with the gigantic lists of numbers you’re putting it memory.
xrangeinstead ofrangewill help fix that issue but you’re still using a terrible benchmark. the loop is going to execute over and over and not actually do anything, so the cpu is busy checking the condition and entering the loop.As you can see, creating these lists is taking the majority of the time here