Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like write a python code, with all necessary limits. Say, python code should consume Memory(x MB), Source (y KB) and Time (z Seconds).

Can I set these attributes to my code and also log the actual Memory, Source and Time of code along with detailed memory of all objects(size) used in code ?

Example Code :
<pre lang="Python">def test(n):
       some_val = 0
       some_dict = {}
       some_list = []
       for i in range(n):
          some_list.append(i)
          some_dict[i] = i*i
      print(some_val)
      print(some_list)
      print(some_dict)

n = int(input())
test(n)

For above example code, how can I set Memory, Source & Time limit ? And how can I print actual same details for above code along with detailed memory of all objects(size) used in code ?

I am learning to become better and efficient programmer. I appreciate your support and help. Is this possible ? Can anyone guide here ?

What I have tried:

 def test(n):
       some_val = 0
       some_dict = {}
       some_list = []
       for i in range(n):
          some_list.append(i)
          some_dict[i] = i*i
      print(some_val)
      print(some_list)
      print(some_dict)

n = int(input())
test(n)
Posted
Updated 19-Aug-20 20:12pm
v2

1 solution

As far as I know, you can't really do that - it wouldn't be meaningfully anyway.
You could check the source code by reading the file information for your source since Python is interpreted not compiled, but that's about it.

Time in seconds? On which machine? The same source can run on an Amstrad 6140 with an 8MHz processor with but one thread and a Intel Core i9-10900K with ten cores, twenty threads and a clock running at 5.3GHz. One will do a lot more in five seconds that the other!
Even if you did, you'd need to talk to the OS to limit it, and that would depend on the OS your app was running on: Windows would be different to MacOs, and again different to Linux - all of which could run the same python app.

Memory? Why? If you want to control your memory, then fit less to to your machine, as Python has no mechanism to restrict that - you could possibly limit a processes memory, but then again you are back to talking direct to the OS.

Why do you think you need this?
 
Share this answer
 
Comments
stackguru 20-Aug-20 10:26am    
Thanks for responding @OriginalGriff
My intention is, If I run a python code that should display the Memory & Time consumed along with source-code-size. Is it not possible ?
stackguru 20-Aug-20 10:30am    
Have many such python scripts. Would like to keep track and optimizing. Like automating these stuffs. Whenever a python script is executed it should display Memory Time and Source-code-size of that particular script.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900