How to profile your C code using gprof and linux perf

Поділитися
Вставка
  • Опубліковано 4 гру 2024

КОМЕНТАРІ • 12

  • @antshivrobotics
    @antshivrobotics  2 роки тому +1

    If you liked the video, please subscribe and turn notifications on to not miss any future videos, thank you!

  • @farzamdorostkar7816
    @farzamdorostkar7816 2 роки тому +2

    Nice video, thanks! It would be great if you made the same tutorial for memory profiling.

  • @aymenkaze1539
    @aymenkaze1539 2 роки тому +1

    thank you so much man insane video :)

  • @xinking2644
    @xinking2644 2 роки тому

    Good video, thank you sir. subscribed!

  • @SuperWhatusername
    @SuperWhatusername 2 роки тому

    Thank you

  • @michaeldunlavey6015
    @michaeldunlavey6015 2 роки тому

    Nice job, but I'm a contrarian. Sorry. I only care about the overall time and how to reduce it. To find out what to fix, I use stackshots. To get a stackshot, run under gdb, hit ^C, thread 1, and bt. If it doesn't run long enough, wrap a temporary loop around the outside.
    So what do I get?
    stack 1. in timewaster, no surprise.
    Fix: comment out timewaster
    stack 1. in printf, no surprise.
    Fix: comment out printf
    Result: loop 100e6 times, 12 sec. (120 nsec)
    stack 1. at line a[i] = fib3(i-1) + fib3(i-2);
    stack 2. at line int fib3(int i){
    i.e. Spends all its time in the function call
    Fix: So get rid of the function call. Do a[j]=a[j-1]+a[j-2].
    Now it takes 5 sec (50 nsec - 2 times faster than before).
    Usually, I take several stackshots, but less than 20. The point is, if something you can do will save X% of time, then it will be on at least X% of stackshots. That's not timing, it's identifying speedups.
    Added: as I think about it, the above code is going to spend most of its time indexing the a array. Not necessary. The inner loop could look something like this: int x, y=1, z=1;
    x = y+z; y = x+z; z = x+y;
    and when you get to the end, grab the right one.

  • @ronensuperexplainer
    @ronensuperexplainer 2 роки тому

    These performance profilers are a disgrace compared to the Microsoft Visual Studio Performance Profiler

    • @antshivrobotics
      @antshivrobotics  2 роки тому

      Interesting. In what way?

    • @ronensuperexplainer
      @ronensuperexplainer 2 роки тому +1

      @@antshivrobotics A GUI that gives line-by-line analysis of exactly which lines of code are taking a long time to run, with red highlighting of the problematic lines of code. That together with a function stack view that lets you "expand hot path".
      Visual Studio performance profiler lets you double-click a function name to see the code of that function with an analysis of each and every line of code.
      It works great with the Release build of your application! (not only debug)
      Essentially all AAA game devs use C++ and use Visual Studio for performance profiling. It even has GPU performance profiling.

    • @bhimsend1581
      @bhimsend1581 2 роки тому +1

      @@ronensuperexplainer I second it. For large projects, I would recommend Visual studio.

    • @TheLordoftheDarkness
      @TheLordoftheDarkness 5 місяців тому +1

      There are things a CLI do better than a GUI. Profiling and debugging are not part of those things. We have to admit that no tool comes even close to Visual studio when it comes to debugging and especially profiling.