epezent / implot

Immediate Mode Plotting
MIT License
4.66k stars 520 forks source link

Demo: suggestion to tweak speed of HeatMap and Histogram2D sections #348

Closed ocornut closed 2 years ago

ocornut commented 2 years ago

The heatmap and histogram2d sections of the demo are unusually "slow" in the default setup, as in that on a desktop machine in Debug build I get huge slowdown on them.

Was wondering if, for the sake of user friendlyness, those could be toned a bit?

In ShowDemo_Heatmaps()
const int size = 200; This is squared so 40000 items. Changing to 80 makes it 6.25 times faster.

In ShowDemo_Histogram2D():

void ShowDemo_Histogram2D() {
    static int count     = 500000;
    static int xybins[2] = {200,200};
    static bool density2 = false;
    ImGui::SliderInt("Count",&count,100,500000);
    ImGui::SliderInt2("Bins",xybins,1,500);
    ImGui::SameLine();
    ImGui::Checkbox("Density##2",&density2);
    static NormalDistribution<500000> dist1(1, 2);
    static NormalDistribution<500000> dist2(1, 1);
    double max_count = 0;
    ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground;
    ImPlot::PushColormap("Hot");
    if (ImPlot::BeginPlot("##Hist2D",ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0))) {
        ImPlot::SetupAxes(NULL, NULL, flags, flags);
        ImPlot::SetupAxesLimits(-6,6,-6,6);
        max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotRect(-6,6,-6,6));
        ImPlot::EndPlot();
    }
    ImGui::SameLine();
    ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0));
    ImPlot::PopColormap();
}

to e.g.

void ShowDemo_Histogram2D() {
    static int count     = 50000;
    static int xybins[2] = {100,100};
    static bool density2 = false;
    ImGui::SliderInt("Count",&count,100,100000);
    ImGui::SliderInt2("Bins",xybins,1,500);
    ImGui::SameLine();
    ImGui::Checkbox("Density##2",&density2);
    static NormalDistribution<100000> dist1(1, 2);
    static NormalDistribution<100000> dist2(1, 1);
    double max_count = 0;
    ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground;
    ImPlot::PushColormap("Hot");
    if (ImPlot::BeginPlot("##Hist2D",ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0))) {
        ImPlot::SetupAxes(NULL, NULL, flags, flags);
        ImPlot::SetupAxesLimits(-6,6,-6,6);
        max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotRect(-6,6,-6,6));
        ImPlot::EndPlot();
    }
    ImGui::SameLine();
    ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0));
    ImPlot::PopColormap();
}

Not sure if there's a specific intent but those changes make those demo sections meaningfully faster.

epezent commented 2 years ago

No particular reason we can't make the proposed change; this was just hastily written example code. Will incorporate this change in a future PR.