0

I've been practicing my C++ programming on LeetCode and whenever I submit a solution it will tell me how long my program took to run and how much memory it used.

I'm using a mac and VSCode with g++ to compile my program locally. I want to find a tool or method I can use to get the same information about the run time and memory usage of my program so I can try tweaking it to see the effect on performance.

Is there a compiler option or something like a command line tool or VSCode extension I can run my program through or would I have to add code to my program to track the time and memory itself?

Devin Crossman
  • 7,454
  • 11
  • 64
  • 102
  • Back in the Day, `Xcode > Open Developer Tool > Instruments` was your best bet. I don't have a Mac, so I don't follow Current Events very closely, but Q: Is XCode not an option these days? – paulsm4 Jul 05 '19 at 19:41
  • @paulsm4 I'm sure XCode is powerful IDE for lots of things and especially if you're doing development specifically for apple hardware, however I usually prefer a lightweight editor like VSCode or Sublime Text and using the terminal for most of my work. – Devin Crossman Jul 05 '19 at 19:49
  • If you want "lightweight", use vi ;) My preference is either a full-fledged IDE (Eclipse, MSVS or Android Studio come immediately to mind) or a text editor. Otherwise, it's "notepad++" or "vi". And "make" or "gradle" ;) Anyway - the reason I brought up XCode is that it explicitly has the tools you need. Or you can use gcc tools like [gprof](http://sourceware.org/binutils/docs/gprof/) (part of binutils, commonly installed alongside gcc). – paulsm4 Jul 05 '19 at 21:39
  • @paulsm4 unfortunately it seems gprof doesn't work on macOS Mojave :( – Devin Crossman Jul 05 '19 at 22:12
  • Fair enough. I still think trying XCode might be the fastest, easiest, most robust solution. But Valgrind/callgrind is certainly worth a look, and to the extent it's CLI based, it should be pretty easy to integrate with VSCode: http://valgrind.org/docs/manual/cl-manual.html. – paulsm4 Jul 05 '19 at 23:09

2 Answers2

5

For simple outputs, you can use gnu time.

As suggested on that page:

/usr/bin/time -f "time result\ncmd:%C\nreal %es\nuser %Us \nsys  %Ss \nmemory:%MKB \ncpu %P" <command>

For a version on Mac OSX, just take a look on this answer: https://apple.stackexchange.com/a/235404

Amadeus
  • 10,199
  • 3
  • 25
  • 31
4

You can use valgrind to find memory leaks or profiling. And Google benchmark for code snippet benchmarks. Or you can measure time yourself with chrono. And an online benchmark tool, quick-bench

Update:

I found this nice info on godbolt which gives insights on the number of iterations or cycles...

Thanks to @Peter the tool llvm-mca estimates the Instructions Per Cycle (IPC), as well as hardware resource pressure. Which is a simulation on a theoretical model of the CPU, not a profile but still could be useful. It also does not cover cache miss.

Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • Thanks, unfortunately valgrind and gprof aren't working on macOS Mojave (yet?), Google benchmark works but doesn't show the memory usage (as far as I can tell), quick-bench seems to only be for relative comparison benchmarking because it's running on AWS with unknown load. Looks like I will have to install xcode to use its Instruments profiler but it feels like overkill. – Devin Crossman Jul 05 '19 at 22:11
  • @DevinCrossman no problem. sees valgrind is out for Mojave https://bugs.kde.org/show_bug.cgi?id=399584 they always have to wait till Apple releases the kernels. Hope that helps – Oblivion Jul 06 '19 at 05:26
  • LLVM-MCA is useful, but it's a simulation on a theoretical model of the CPU (I think assuming no cache misses, [like IACA](https://stackoverflow.com/questions/26021337/what-is-iaca-and-how-do-i-use-it)), *not* a profile. – Peter Cordes Jul 06 '19 at 21:00
  • @PeterCordes thanks I edited. You are right they stated that the cache hit or miss is not covered. – Oblivion Jul 06 '19 at 21:29