# How to get a benchmark report of all called function?

**URL:** https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337
**Category:** Help & Support
**Created:** [January 18, 2024, 4:40pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337 "2024-01-18T16:40:11Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 18, 2024, 4:40pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/1 "2024-01-18T16:40:11Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)
#### Post date: [January 18, 2024, 4:45pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/2 "2024-01-18T16:45:45Z")

</div>

> [@aboulafia](#):
>
> Is it a Crystal thing, or must I use an external tool ?

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.

---

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 18, 2024, 5:07pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/3 "2024-01-18T17:07:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 18, 2024, 5:49pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/4 "2024-01-18T17:49:05Z")

</div>

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 ;-)

---

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 18, 2024, 6:16pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/5 "2024-01-18T18:16:30Z")

</div>

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)

```auto
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 !

---

<div class="post-metadata">

### Author: ![drmidnight](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drmidnight/32/2266_2.png) [@drmidnight](https://forum.crystal-lang.org/u/drmidnight)
#### Post date: [January 18, 2024, 8:09pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/6 "2024-01-18T20:09:46Z")

</div>

I use Instruments.app 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.

---

<div class="post-metadata">

### Author: ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)
#### Post date: [January 18, 2024, 8:23pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/7 "2024-01-18T20:23:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![drmidnight](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drmidnight/32/2266_2.png) [@drmidnight](https://forum.crystal-lang.org/u/drmidnight)
#### Post date: [January 18, 2024, 8:29pm UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/8 "2024-01-18T20:29:49Z")

</div>

I just do the following:

- build with --release
- open Instruments.app
- 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.

---

<div class="post-metadata">

### Author: ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)
#### Post date: [January 19, 2024, 12:40am UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/9 "2024-01-19T00:40:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![drmidnight](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drmidnight/32/2266_2.png) [@drmidnight](https://forum.crystal-lang.org/u/drmidnight)
#### Post date: [January 19, 2024, 4:54am UTC](https://forum.crystal-lang.org/t/how-to-get-a-benchmark-report-of-all-called-function/6337/10 "2024-01-19T04:54:00Z")

</div>

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