// (C) Marcin Borkowski 1998, borek@bpp.home.pl
// model Large, opimization set to: for speed, calling convention: registers,
// instruction set 8088/86, stack options: none. These are the fastest
// settings for this task, this memory model, this compiler (Borland C++ 3.1)
// and Pentium II processor.

// Result in 'FiboMHertz' was normalized to show 233 on Pentium II 233.
// (BTW: if you remove division from the printf() line, program will be
// a little bit slower. Why? Ask Borland, their complicator, their problem :)
 
// One more remark - please, stop sending me information about how to speed up
// this code, and why recursive implementation is unefficient. Do you really think
// I don't know how to make the same thing in a loop? Recursion produces tons of
// work for processor, and that's exactly what is necessary for any kind of benchmark,
// even for a stupid one.

#include <bios.h>
#include <stdio.h>

long fibonacci(int i)
{
   if (i > 0) return (fibonacci(i-1)+fibonacci(i-2));
         else return 1l;
// Due to the way Pentium II and Pro predicts branches (conditional not to be taken)
// following code is a little bit slower on those processors (about 7%), as usually
// i is greater then 0. But I'm not as cruel as Intel may expect.
//   if (i <= 0) return 1l;
//         else return (fibonacci(i-1)+fibonacci(i-2));
}

void main(void)
{
   long start_time,elapsed;

   printf("Fibonacci benchmark, (C) Marcin Borkowski 1998\r\n");
   start_time = biostime(0,0l);
   fibonacci(40);
   elapsed = biostime(0,0l)-start_time;
   printf("Elapsed time: %li clock ticks (%li FiboMHertz)\r\n",elapsed,371169/elapsed);
}

/*
Here is disassembled version of fibonacci function (for a large model):

fibonacci: long fibonacci(int i)
  cs:0005 55             push   bp
  cs:0006 8BEC           mov    bp,sp
  cs:0008 56             push   si
  cs:0009 8B7606         mov    si,[bp+06]
#FIBON#10:  if (i > 0) return (fibonacci(i-1)+fibonacci(i-2))
  cs:000C 0BF6           or     si,si
  cs:000E 7E24           jle    #FIBON#11 (0034)
  cs:0010 8BC6           mov    ax,si
  cs:0012 48             dec    ax
  cs:0013 50             push   ax
  cs:0014 0E             push   cs
  cs:0015 E8EDFF         call   fibonacci
  cs:0018 59             pop    cx
  cs:0019 50             push   ax
  cs:001A 52             push   dx
  cs:001B 8BC6           mov    ax,si
  cs:001D 05FEFF         add    ax,FFFE
  cs:0020 50             push   ax
  cs:0021 0E             push   cs
  cs:0022 E8E0FF         call   fibonacci
  cs:0025 59             pop    cx
  cs:0026 8BDA           mov    bx,dx
  cs:0028 5A             pop    dx
  cs:0029 59             pop    cx
  cs:002A 03C8           add    cx,ax
  cs:002C 13D3           adc    dx,bx
  cs:002E 8BC1           mov    ax,cx
  cs:0030 EB09           jmp    #FIBON#12 (003B)
  cs:0032 EB07           jmp    #FIBON#12 (003B)
#FIBON#11:  else return 1l;
  cs:0034 33D2           xor    dx,dx
  cs:0036 B80100         mov    ax,0001
  cs:0039 EB00           jmp    #FIBON#12 (003B)
#FIBON#12: }
  cs:003B 5E             pop    si
  cs:003C 5D             pop    bp
  cs:003D CB             retf

*/
 

Back