# Measure execution time

## CPP

```cpp
#include <string>
#include <chrono>
#include <iostream>

namespace ch = std::chrono;

class ScopeTimer final {
public:
    ScopeTimer(const std::string& name)
        : m_name(name), m_start(ch::high_resolution_clock::now()) {
        std::cout << m_name << " start" << std::endl;
    }

    ~ScopeTimer() {
        const auto end = ch::high_resolution_clock::now();
        const auto duration = ch::duration_cast<ch::milliseconds>(end - m_start).count();
        std::cout << m_name << " end, duration: " << duration << " ms" << std::endl;
    }

private:
    const std::string m_name;
    const ch::high_resolution_clock::time_point m_start;
};

#define SCOPE_TIMER(name) ScopeTimer timer##__LINE__(name)

int fibonacci(int n) {
    return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
    {
        SCOPE_TIMER("fibonacci_10");
        fibonacci(10);
    }

    {
        SCOPE_TIMER("fibonacci_30");
        fibonacci(30);
    }

    {
        SCOPE_TIMER("fibonacci_40");
        fibonacci(40);
    }

    {
        SCOPE_TIMER("fibonacci_50");
        fibonacci(50);
    }

    return 0;
}

```

&#x20;Время в Output будет зависеть от вашего CPU:

```
fibonacci_10 start
fibonacci_10 end, duration: 0 ms
fibonacci_30 start
fibonacci_30 end, duration: 5 ms
fibonacci_40 start
fibonacci_40 end, duration: 622 ms
fibonacci_50 start
fibonacci_50 end, duration: 85770 ms
```

## Unreal Engine

{% embed url="https://docs.unrealengine.com/4.26/en-US/TestingAndOptimization/PerformanceAndProfiling/StatCommands/StatsSystemOverview/" %}

```
SCOPE_LOG_TIME(Name, CumulativePtr)

SCOPE_LOG_TIME_IN_SECONDS(Name, CumulativePtr)

SCOPE_LOG_TIME_FUNC()

SCOPE_LOG_TIME_FUNC_WITH_GLOBAL(CumulativePtr)

...
```
