ScottPlot / ScottPlot/ScottPlot

Histogramm: Optimize runtime by using better Add function for Histogramm

Open
#4,860 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.8k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Suggestion:
The runtime of the add function is very slow if it is nessesary to set 100000 Bins or more.
To optimize the runtime of the Add function in Histogramm.Add(double value) i suggest this code.
I test the code with 100000 items in the Edges array and the values are the same as before.


/// <summary>
/// A histogram that accumulates the number of values observed in a continuous range of user defined bins
/// </summary>
public class Histogram
{
.....
        public void Add(double value)
        {
            if (value < Edges[0])
            {
                if (IncludeOutliers)
                    Counts[0]++;
                return;
            }

            int lastIndex = Edges.Length - 1;
            if (value > Edges[lastIndex])
            {
                if (IncludeOutliers)
                    Counts[^1]++;
                return;
            }

            int index = Array.BinarySearch(Edges, value);

            if (index < 0)
                index = ~index - 1; 

            // Ensure the last edge is included in the last bin
            if (index == lastIndex)
                Counts[Counts.Length - 1]++;
            else
                Counts[index]++;
        }
...
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Histogram.Add(double value), the method identified in the issue, and compare its current bin lookup with the proposed Array.BinarySearch approach. Validate that adding values with around 100000 entries in Edges produces the same Counts, including outliers and the final edge, while improving runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
data-visualization, performance
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.