Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

look to tracy profiler or any profiler #474

Open
sea-kg opened this issue Oct 27, 2022 · 0 comments
Open

look to tracy profiler or any profiler #474

sea-kg opened this issue Oct 27, 2022 · 0 comments
Assignees

Comments

@sea-kg
Copy link
Member

sea-kg commented Oct 27, 2022

https://luxeengine.com/integrating-tracy-profiler-in-cpp/

OR

#include <iostream>
#include <unordered_map>
#include <chrono>
#include <vector>

thread_local struct DebugPoints {
    struct DebugPointInfo  {
        DebugPointInfo() {};
        long counter = 0;
        const char *filename = 0;
        const char *mark = 0;
        int fileline = 0;
        std::vector<long int> times = {};
    };

    std::unordered_map<std::size_t, DebugPointInfo> points;
    std::chrono::time_point<std::chrono::high_resolution_clock> tm = std::chrono::high_resolution_clock::now();

    std::size_t add_point(const char *filename, int fileline, bool bPrint = true) {
        std::size_t hash = std::hash<const char *>()(filename);
        hash -= fileline;
        int cntr = ++(this->points[hash].counter);
        if (cntr == 1) {
            this->points[hash].filename = filename;
            this->points[hash].fileline = fileline;
        }
        auto prev_tm = this->tm;
        this->tm = std::chrono::high_resolution_clock::now();
        std::chrono::duration<long int, std::nano> ms_double = this->tm - prev_tm;
        if (bPrint) {
            std::cout << filename << ":" << fileline << ": cntr=" << cntr << "; time=" << ms_double.count() << "ns;" << std::endl;
        }
        this->points[hash].times.push_back(ms_double.count());
        return hash;
    }

    std::size_t add_point(const char *filename, int fileline, const char *mark, bool bPrint = true) {
        std::size_t hash = add_point(filename, fileline, bPrint);
        this->points[hash].mark = mark;
        return hash;
    }

    void print_avarage(std::size_t hash) {
        long int sumtime = 0;
        for (int i = 0; i < this->points[hash].times.size(); ++i) {
            sumtime += this->points[hash].times[i];
        }
        std::cout
            << this->points[hash].filename << ":" << this->points[hash].fileline << ": "
        ;
        if (this->points[hash].mark) {
            std::cout << " (" << this->points[hash].mark << ") ";
        }
        std::cout
            << "cntr=" << this->points[hash].counter << "; "
        ;
        if (this->points[hash].counter > 1) {
            std::cout << "avarage_time=";
        } else {
            std::cout << "time=";
        }
        std::cout 
            << (sumtime / this->points[hash].counter) << "ns;"
            << std::endl
        ;
    }

    void print_all() {
        std::cout << ">>>>>>>>> PANDUS RESULTS BEGIN" << std::endl;
        for (auto it = this->points.begin(); it != this->points.end(); it++) {
            this->print_avarage(it->first);
        }
        std::cout << "<<<<<<<<< PANDUS RESULTS END" << std::endl;
    }

} g_pandus;

#define PANDUS_POINT() g_pandus.add_point(__FILE__, __LINE__, true);
#define PANDUS_POINT_MARK(__MARK__) g_pandus.add_point(__FILE__, __LINE__, #__MARK__, true);
#define PANDUS_POINT_SILENT() g_pandus.add_point(__FILE__, __LINE__, false);
#define PANDUS_POINT_MARK_SILENT(__MARK__) g_pandus.add_point(__FILE__, __LINE__, #__MARK__, false);

int main()
{
    int sum = 0;

    PANDUS_POINT_MARK(main);
    sum = 0;
    for (int i = 0; i < 100000; ++i) {
        sum += 100;
    }
    PANDUS_POINT();
    sum = 0;
    std::size_t _hash;
    PANDUS_POINT();
    for (int i = 0; i < 10000; ++i) {
        sum += 100;
        _hash = PANDUS_POINT_MARK_SILENT(sum 100);
    }
    g_pandus.print_avarage(_hash);

    PANDUS_POINT();

    g_pandus.print_all();
    return 0;
}
@sea-kg sea-kg self-assigned this Oct 27, 2022
@sea-kg sea-kg changed the title look to tracy profiler look to tracy profiler or any profiler Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant