[fpc-pascal] Happy tickets benchmark

wkitty42 at windstream.net wkitty42 at windstream.net
Wed Feb 17 18:58:59 CET 2016


On 02/17/2016 06:28 AM, Serguei TARASSOV wrote:
> On 17/02/2016 12:00, fpc-pascal-request at lists.freepascal.org wrote:
>> small remark for your testing series: AVG makes no sense, you should test
>> against MIN  - why ? the measured results are contaminated by other
>> activities on your system, so the fastest result is the most accurate,
>> because there is no way to make a program to run faster, but many ways to
>> make it run slower.
> No, the test against MIN shows only the case when the result was _minimally
> contaminated_ in the series. But we don't know whether the unused time was
> bigger or smaller than for other program.

i thought about this, too... especially considering starting the test's binary 
from the command line each time and how disk caching may affect loading and 
unloading...

> Also, it is very probably that the minimal time in series of 1000 will be
> better that in series of 10 and so on.
>
> The average approach smooth the contaminated time in series for all
> programs. But you could use some better approaches like the direct measure of
> the time used by CPU or simply remove extreme values.

one might also have the program go a little further than a simple one-time 
execution and have it loop internally while keeping up with the minimum, maximum 
and average times and output them at the end in addition to the other output in 
the test itself... [code below]

> I don't suppose that it changes anything in the relative comparison that is
> the goal of test.

possibly not...


===== snip =====
program HappyTickets;

uses
   SysUtils, DateUtils;

var
   loopcnt : integer;
   elapsedtime, mintime, maxtime, avgtime: int64;


   procedure runtest;

   var
     n1, n2, n3, n4, n5, n6, n7, n8: 0..9;
     TicketsCount: int64;
     d1, d2: TDateTime;
   begin
     TicketsCount := 0;
     elapsedtime := 0;
     d1 := Now;
     for n1 := 0 to 9 do
       for n2 := 0 to 9 do
         for n3 := 0 to 9 do
           for n4 := 0 to 9 do
             for n5 := 0 to 9 do
               for n6 := 0 to 9 do
                 for n7 := 0 to 9 do
                   for n8 := 0 to 9 do
                     if n1 + n2 + n3 + n4 = n5 + n6 + n7 + n8 then
//                    if n1 + n2 + n3 + n4 - n5 - n6 - n7 in [0..9] then
                       Inc(TicketsCount);
//                      TicketsCount += 1;
//                      TicketsCount := TicketsCount + 1;
     d2 := Now;
     elapsedtime := DateUtils.MilliSecondsBetween(d1, d2);
     writeln('Round ',loopcnt:4,' Found ', TicketsCount, ' tickets. Elapsed 
time, msec: ',elapsedtime );
   end;

begin
   elapsedtime := 0;
   mintime := 0;
   maxtime := 0;
   avgtime := 0;
   for loopcnt := 1 to 1000 do
     begin
       runtest;
       if mintime = 0 then mintime := elapsedtime;
       if maxtime = 0 then maxtime := elapsedtime;
       if avgtime = 0 then avgtime := elapsedtime
       else avgtime := (avgtime + elapsedtime) div 2;
       if elapsedtime < mintime then mintime := elapsedtime;
       if elapsedtime > maxtime then maxtime := elapsedtime;
       writeln('Min: ',mintime:5,'   Max: ',maxtime:5,'   Avg: ',avgtime);
     end;
end.

===== snip =====

-- 
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list* unless
        private contact is specifically requested and granted.



More information about the fpc-pascal mailing list