Advertisement
WarPie90

histo - press timings

Jan 30th, 2023 (edited)
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.04 KB | None | 0 0
  1. program new;
  2. {.$I SRL/osr.simba}
  3.  
  4.  
  5. function TruncatedGauss(Left:Double=0; Right:Double=1; CUTOFF:Single=0): Double;
  6. begin
  7.   if CUTOFF <= 0 then CUTOFF := 4.5;
  8.  
  9.   Result := CUTOFF+1;
  10.   while Result >= CUTOFF do
  11.     Result := Abs(Sqrt(-2 * Ln(Random())) * Cos(2 * PI * Random()));
  12.   Result := Result / CUTOFF * (Right-Left) + Left;
  13. end;
  14.  
  15. function NormalRange(Min, Max: Int64; CUTOFF:Single=0): Int64;
  16. begin
  17.   if CUTOFF <= 0 then CUTOFF := 4.5;
  18.   case Random(0,1) of
  19.     0: Result := Round((Max+Min)/2.0 + TruncatedGauss(0, (Max-Min)/2, CUTOFF));
  20.     1: Result := Round((Max+Min)/2.0 - TruncatedGauss(0, (Max-Min)/2, CUTOFF));
  21.   end;
  22. end;
  23.  
  24. {$ifndecl GaussRand}
  25. function GaussRand(Mean, Dev: Double): Double;
  26. var
  27.   len: Double;
  28. begin
  29.   len := Dev * Sqrt(-2 * Ln(Random()));
  30.   Result := Mean + len * Cos(2 * PI * Random());
  31. end;
  32. {$endif}
  33.  
  34.  
  35. var
  36.   a: array of Double;
  37.   log,srl: TDoubleArray;
  38.  
  39. procedure TMufasaBitmap.DrawBoxBlend(b: TBox; Color: Int32);
  40. var
  41.   x,y: Int32;
  42. begin
  43.   for y:=b.y1 to b.y2 do
  44.     for x:=b.x1 to b.x2 do
  45.     try
  46.       Self.SetPixel(x,y, color xor Self.GetPixel(x,y));
  47.     except end;
  48. end;
  49.  
  50.  
  51. procedure PlotSimpleHistogram(bmp: TMufasaBitmap; list: TDoubleArray; lo,hi:Int32; color: Int32);
  52. var
  53.   i,x,y,h:Int64;
  54.   hfact,wfact,minv,maxv:Double;
  55.   count: TIntegerArray;
  56.   W := 2;
  57. begin
  58.   minv := list.Min();
  59.   maxv := list.Max();
  60.  
  61.   wfact := BMP.GetWidth()  / (hi-lo+1);
  62.  
  63.   SetLength(count, BMP.GetWidth() div W + 1);
  64.   for i:=0 to High(list) do try
  65.     Inc(count[Round((list[i]-lo) * wfact) div W]);
  66.   except end;
  67.  
  68.   h := BMP.GetHeight()-1;
  69.   hfact := (BMP.GetHeight()-50) / (count.max()+1);
  70.   for x:=0 to High(count) do
  71.   begin
  72.     y := Trunc(count[x] * hfact);
  73.     try
  74.       BMP.DrawBoxBlend(Box(x*W, h-y, x*W+(W-2), h), color);
  75.     except
  76.     end;
  77.   end;
  78. end;
  79.  
  80.  
  81. procedure OnTerm();
  82. var
  83.   histo: TMufasaBitmap;
  84. begin
  85.   WriteLn('Standard deviation: ', log.Stddev(),'ms');
  86.   WriteLn('Avg press duration: ', log.Mean(),  'ms');
  87.  
  88.   // histogram
  89.   histo := TMufasaBitmap.Create();
  90.   histo.SetSize(500,350);
  91.   PlotSimpleHistogram(histo, srl, 0,350,$00FF00);
  92.   PlotSimpleHistogram(histo, log, 0,350,$0000FF);
  93.   histo.Show();
  94.   histo.Free();
  95. end;
  96.  
  97. var
  98.   k: Int32;
  99.   kd: Boolean;
  100. begin
  101.   srl := [];
  102.   for 0 to 1000 do
  103.     srl += GaussRand(72,15);
  104.  
  105.   AddOnTerminate(@OnTerm);
  106. end;
  107.  
  108. // capture userdata:
  109. begin
  110.   SetLength(a, 256);
  111.  
  112.   while True do
  113.   begin
  114.     // this loop uses less than 1 ms, so it wont distort the results
  115.     for k:=60 to 160 do
  116.     begin
  117.       if not (Chr(k) in ['a'..'z','A'..'Z', '0'..'9','.',',','-']) then
  118.         continue;
  119.  
  120.       kd := isKeyDown(k);
  121.       if kd and (a[k] = 0) then
  122.         a[k] := PerformanceTimer()
  123.       else if (a[k] <> 0) and (not kd) then
  124.       begin
  125.         log += PerformanceTimer() - a[k];
  126.         a[k] := 0;
  127.       end;
  128.     end;
  129.   end;
  130. end.
  131.  
  132.  
  133. // day 1 (night)
  134. > Standard deviation: 10ms
  135. > Avg press duration: 80ms
  136.  
  137. // day 2 (daytime)
  138. > Standard deviation: 15.8ms
  139. > Avg press duration: 71ms
  140.  
  141. // garrett
  142. 26.8
  143. 116.96
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement