How can I know how much memory my code takes up and how long it takes to run?

Back to General discussions forum

AHIMOTUVWXY0     2024-03-04 09:08:43

How do I know how much memory my code takes up and how long it has been running?

dangarciahe     2024-03-04 23:48:50
User avatar

Hi, AHIMOTUVWXY0 :)

Could you please be more specific? Which language are you using? Are you running your code locally in your computer or here in the servers?

AHIMOTUVWXY0     2024-03-06 05:59:41

I am doing some programming problems on this website, and I want to know the running time and memory usage of the code I wrote.

moxieman     2024-03-09 00:32:52
User avatar

If you're actually executing your code through the website (via the green "Run it!" button), then I'm not sure there's a way for the website to communicate back to you the amount of memory or runtime required to execute your code.

If you're actually executing your code locally on your own machine, then you might check runtime with any built-in timing functions, like millis() for C++, although that will give you the clock time between calls and not necessarily the total amount of CPU time required... Often times that's a good enough estimate.

I know that some IDEs (like Arduino IDE) reports the memory allocated for constants and instructions when you compile, but doesn't tell you anything about RAM usage while running...

After searching a bit with google, apparently you're running locally on Linux/Unix, apparently you can run "$ time -v python yourprogram.py" in the command shell to get a lot of info on the given program, including runtime (clock, user, and system), %CPU allocated during execution, etc. I guess that the "Measure-Command" does something similar on Windows.

For actual memory allocation, after a quick google search it looks like there might some be python libraries which return memory usage info when it was called? Finding max memory allocation over the total runtime might be more difficult, I'm not sure...

Rodion (admin)     2024-03-09 05:32:09
User avatar

你好!

Checking memory size is difficult. Memory management is complicated in modern computers. There are different memory areas (so you can have plenty of heap memory but exhaust stack memory for example). In C++ there is no universal function to check used / remaining memory from the program itself.

It is possible to call top command suggested by Kevin (aka moxieman) with your program's pid, but it's somewhat painful so I won't go into details unless you really want them :)

Also here is a function malloc_usable_size which you can call on any allocated object to try figuring out how much this specific object takes. But to calculate total you'll need to do it for all of them.

So generally it is better to teach yourself calculating used memory in your head e.g. you know how much memory you are allocating. Before C++ in pure C you needed to specify allocated memory size yourself in the call to malloc, so it is not actually very difficult. You know how much simple types take and you can use sizeof operator if something more difficult is needed.


Checking thime is pretty easy, there are various functions to get current time in milli, micro or nanoseconds and you simply call them twice - at beginning and at the end. Here is an example. And print difference.

I see you have switched to Python. It is slightly easier here - I guess there is time.time() function which returns current time in seconds but as fractional number - so you again call it two time and get difference.

Rodion (admin)     2024-03-10 18:10:36
User avatar

Quite suddenly it came into my mind that could be an idea for a problem. Surely it is pretty "programmer's problem", much unlike most others here - but it may teach us something useful and interesting - and particularly it may show how difficult it is to judge about real memory consumption - so hopefully it won't be much harm.

It's live now.

dangarciahe     2024-03-12 00:44:25
User avatar

If using Python, I recommend using Jupyter with nbextensions, as you can see execution time and memory usage.

I once did some testing and the runtime overhead with Jupyter is negible. Memory overhead is bigger, though.

There's also the %timeit command, which does several passes on a single function to give you mean time and uncertainity of runtime. It also automatically scales the number of runs based on runtime, so you won't end up waiting an absurd ammount of time.

Please login and solve 5 problems to be able to post at forum