user1089679
user1089679

Reputation: 2368

Want to notedown CPU usage and Memory Usage of my C program

Here i made one function which calculates CRC16 of any file . i made this function and program in C .

Now i want to find out CPU usage and Memory usage of my this CRC16 calculation function.

I know about top command. here i have no more time to see this thing on other tab and its not user friendly to use this.

i found one link but i cant get more idea about this.

link

Is there any function which do these things and give me result?

Please Any body help me to find out this thing.

Upvotes: 1

Views: 565

Answers (3)

Dervin Thunk
Dervin Thunk

Reputation: 20140

Besides the answers above, you can also use pmap, mmap, oprpfile and, my personal favorite, perf, found in linuxtools. Hope it helps.

Upvotes: 1

Gui13
Gui13

Reputation: 13561

If you are picky about performances and work on x86 or x64, you can try valgrind and its callgrind tool, as well as its more precise (but harder to grasp) cachegrind alternative.

After installing it through your distribution's package manager, it is simply a matter of:

valgrind --tool=cachegrind ./a.out
valgrind --tool=callgrind ./a.out

Note that you can use the visual tool kcachegrind to visialize the performance data easily. I linked to the wikipedia article for valgrind, because it's succint and quite complete. The official Valgrind website is here.

I usually improve my hot loops performance by some orders of magnitude using this tool!

Upvotes: 0

Michael Dillon
Michael Dillon

Reputation: 32392

Type in man 2 getrusage for more info.

I expect that ru_idrss is what you need for memory usage. It should be filled in in more recent versions of the Linux kernel. Or you could use procps and get info from the /proc directory. The difference in ru_utime before and after the calculation will give you the CPU time

There is more info in this question: How to get memory usage at run time in c++?

Upvotes: 1

Related Questions