Skip to main content


Code snipet for Histogram

While working on the next code update, the code example below raised some eyebrows. How much time does it take to compute equidistant histogram? It turnes out, that our old code needed about 3x more time than array copy operation. The current version is a near match, being only 10% slower than best optimized array copy:

[delphi]        Results.SetZero;
        while not Data.BlockEnd do
            aData.ThresholdGT_LT(Data,Max,Max,Min, Min);
            aData.Normalize(aData, Min, BinWidth);
            aData.CopyTo(intData, TRounding.rnTrunc);
            HistoCount(intData.IData, intData.DataIndex(0), Results.IData, Results.dataIndex(0), intData.Length);
//Old code:
//        for i:=0 to Data.Length-1 do
//        begin
//          j:= Trunc((Data.Values[i]-Min)*InvBinWidth); // was round
//          if (j < 0) then Inc(leftTail)
//          else if (j >= NumBins) then Inc(righttail)
//          else Results.IValues[j] := Results.IValues[j] + 1;
//        end;[/delphi] While the vectorization seems dominating, it brings only 20% speedup. The other 250% come in to effect only after block processing is applied. (BlockInit, BlockNext, BlockEnd).

  • Created on .