How to get a benchmark report of all called function?

Hi all,
I wonder how to run the same piece of code thousand times and get a full report of which functions where called, and how cumulated time have been spent in each

Is it a Crystal thing, or must I use an external tool ?

1 Like

You should be able to use the same tools as is used for C and C++ and others. prof is one example on linux, but there are other tools that also works.


I’m on MacOS (with MacPorts) and it seems that the only profiling tool is the one provided by XCode (could not install any profiler from MacPorts, ValGrind fails to install for example)

I tried
xcrun xctrace record --template='Time Profiler' --launch -- myapp_to_bench

Then I got a ***.trace file/directory that I can open with “Instruments” (another XCode tool) but I can’t get any meaningful information

If someone on MacOS can help me

Ok, so the real command line is
xctrace record --template='Time Profiler' --launch -- ./toyl ./p6.ty

And it seems that the ./p6.ty argument is not passed to the executable
it works when hardcoding it and I can see some profiled informations

Still some help needed ;-)

I don’t know if I’m doing something wrong, but seems that my toy compiler can handle a 25 lines source code, 1 million times, so we might say we get 25 millions of lines in less than 24 seconds (including opening and closing the same file 1 million times)

time ./toyl ./p2.ty
./toyl ./p2.ty  23,72s user 4,95s system 98% cpu 29,229 total

That’s crazy !!!

So when someone said that premature optimization is the root of all evil, I believe it !

I use and it works like a charm for profiling crystal, can even jump to the source from the stack trace.

I just directly attach to the process or launch it using Instruments and let it record while it’s running.

1 Like

How do you do ? Launch from command line? Special crystal build flags ?

I just do the following:

  • build with --release
  • open
  • choose System Trace as the profiling template
  • at the top next to the pause button choose your process or “Choose target…”
  • hit the record button

Launching from Instruments by using “Choose target…” gives you some options to pass ENV vars or command line arguments.

If the process ends it will process the trace and show it to you or if it’s a long running process and you need to just grab a bit, record until you have what you want and hit the stop button.

You can use the “+” in the upper right to add other instruments or console output if you need that.


I recently used on macOS for the first time. This is interesting.

There are a lot of different metrics you can collect, not all of them work but the vast majority does.