Interesting, this.

I need to get more speed out of some date mathematics. The following is a well known algorithm (so the way it works, is not important.

VB Code:
  1. Public Function Julian(y As Long, m As Long, d As Long) As Long
  2.  
  3.     Dim jy As Long
  4.     Dim ja As Long
  5.     Dim jm As Long
  6.     Dim intgr As Long
  7.    
  8.     If (m > 2) Then
  9.         jy = y
  10.         jm = m + 1
  11.     Else
  12.         jy = y - 1
  13.         jm = m + 13
  14.     End If
  15.    
  16.     intgr = Fix(Fix(365.25 * jy) + Fix(30.6001 * jm) + d + 1720995)
  17.    
  18.     ja = Fix(0.01 * jy)
  19.     intgr = intgr + 2 - ja + Fix(0.25 * ja)
  20.    
  21.     CGreg2Jul = intgr
  22.    
  23. End Function

So, ignoring the subtleties of the 'FIX' function, I rewrote it in C and housed it in a DLL as:

VB Code:
  1. int _stdcall Julian (int y, int m, int d)
  2. {
  3.  
  4.     int jy,ja,jm,intgr;
  5.  
  6.  
  7.     if (m>2)
  8.     {
  9.         jy=y;
  10.         jm=m+1;
  11.     }
  12.     else
  13.     {
  14.         jy=y-1;
  15.         jm=m+13;
  16.     };
  17.  
  18.     ja = int(0.01*jy);
  19.     intgr = int(int(365.25*jy)+int(30.6001*jm)+d+1720995);
  20.     intgr = intgr+2-ja+int(0.25*ja);
  21.  
  22.     return intgr;
  23. };

I was hoping, by default, for a marvellous jump in speed, here. But I didn't get it. I tried two things:

(i) Compiled, and calling the function using a Declare stmt = virtually identical speed. On some runs the C DLL perfoms around 20% better
(ii) Through a typelib . . .

VB Code:
  1. [
  2.     uuid(ad534fb5-794a-11da-8cd6-0800200c9a66),
  3.     version(1.0),
  4.     helpstring("Date Win32 Library")
  5. ]
  6. library Date
  7. {
  8.  
  9.     [dllname("Date.dll")]
  10.  
  11.     module Date_API
  12.     {
  13.  
  14.         [entry("Julian")]
  15.         long Julian ([in] int y,[in] int m,[in] int d);
  16.  
  17.     }
  18. }

. . . which consistently gets around 20% improvement in speed, but sometimes jumps to about 40% improvement.

I was hoping for >100% improvement in speed.

Just goes to show, that the architecture (ie how it's put together) in some cases is much more important than the raw code.

Any thoughts?