XBase function vs C function

271 views
Skip to first unread message

Luigi Ferraris

unread,
Sep 23, 2025, 4:56:40 AM (3 days ago) Sep 23
to Harbour Users
Hi friends,
I need your opinion about this question:
I have a public function used many times.
This function normalize degrees to be ensure value is in the range 0 <= x <= 360
At this moment I'm using a Xbase function.
Is there any benefit to write as C function?

Many thanks in advance
Luigi Ferraris

Francesco Perillo

unread,
Sep 23, 2025, 11:07:31 AM (3 days ago) Sep 23
to harbou...@googlegroups.com
Hi Luigi,
It is impossible to have a quick answer. In these cases the number of times you call a function is more important than the speed of the function.

And the only way to discover if it is valuable to create a c function is to create it and do some speed tests... with real data




--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/76621126-9933-4248-a7f4-2c484cd6c678n%40googlegroups.com.

marcos...@gmail.com

unread,
Sep 23, 2025, 12:34:21 PM (3 days ago) Sep 23
to Harbour Users
Hello 
Luigi,

From what I understand, the process you're performing is not very complex, but everything should be tested empirically.
Any process is faster in C than in Harbour. If it's called many times, you might gain an advantage, but you should run a benchmark first to determine the time saved per call and whether it's worthwhile.

For example, I ran an empty loop that repeated 80 million times in Harbour and in C, and these were the results:

Speed comparison: Harbour vs C
======================================
Empty for loop of 80,000,000 iterations

Harbour time: 5.64000000 seconds
C time:       0.01600000 seconds

C/Harbour ratio: 352.50 times faster

Only through testing can we analyze whether it's convenient or not.

Regards,

Marco Jarrin


marcos...@gmail.com

unread,
Sep 23, 2025, 12:41:44 PM (3 days ago) Sep 23
to Harbour Users
Note: The C code is embedded within Harbour and invoked from Harbour, enabling a direct performance comparison between the two execution environments.

Francesco Perillo

unread,
Sep 23, 2025, 2:03:09 PM (3 days ago) Sep 23
to harbou...@googlegroups.com
The C compiler may actually completely remove the loop in the optimizing phase. Clipper, as far as I know, doesn't have such a optimizer pipeline....

@Luigi, please post the code so that we may have a look...

Luigi Ferraris

unread,
Sep 24, 2025, 5:35:06 AM (2 days ago) Sep 24
to Harbour Users
Hi Francesco,
this is the XBase code
FUNCTION lw_NormalizeAngle( arg1 )
   arg1 := hb_DefaultValue( arg1, 0 )
RETURN( (arg1 % 360 + 360) % 360 )

while this is C (I was inspired googling)
HB_FUNC( LW_NORMDEG360 )
{
   if( HB_ISNUM( 1 ) )
   {
      double angle = hb_parnd( 1 );
      // Normalize to the range [-360, 360)
      angle = fmod(angle, 360.0);
      // If the result is negative, add 360 to bring it into the [0, 360) range
      if( angle < 0)
         angle += 360.0;
     
      hb_retnd( angle );
      return;
   }

   hb_errRT_BASE_SubstR( EG_ARG, EG_ARG, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}


Best regards
Luigi

pete....@gmail.com

unread,
Sep 24, 2025, 12:41:51 PM (2 days ago) Sep 24
to Harbour Users
Ok, below are refined versions of your functions plus one pseudofunction (on top, as #xtranslate).
You can speedtest them  by encosing them into very long repetitive loops
but  I'm pretty sure the speed differences you'll find will be negligible.
Maybe the C version could be a little bit faster but only for really huge number of invocations, 
although, most probably, the pseudofunction would follow very close so it could be the best candidate
for ordinary use. 

// code --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#xtranslate _Normalize_Angle( <nDegree> ) => ( ( ( <nDegree> % 360 ) + 360 ) % 360 )
PROC Main( angle )

angle := Val( hb_defaultValue( angle, "0" ) ) + 0.00

CLS

? "angle       =", angle
? "Func       =>", lw_NormalizeAngle( angle )
? "C_Func     =>", LW_NORMDEG360( angle )
? "PseudoFunc =>", _Normalize_Angle( angle )


FUNCTION lw_NormalizeAngle( arg1 )
RETURN ( ( ( arg1 % 360 ) + 360 ) % 360 )


#pragma BEGINDUMP

#include "hbapi.h"
#include "hbapierr.h"
#include "math.h"


HB_FUNC( LW_NORMDEG360 )
{
   if( HB_ISNUM( 1 ) )
   {
hb_retnd( fmod( fmod( hb_parnd( 1 ), 360.0 ) + 360.0, 360.0 ) );

      return;
   }

   hb_errRT_BASE_SubstR( EG_ARG, EG_ARG, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}

#pragma ENDDUMP
// end code --------------------------------------------------------------------------------------------------------------------------------------------------------------------------

marcos...@gmail.com

unread,
Sep 24, 2025, 4:35:23 PM (2 days ago) Sep 24
to Harbour Users
Hello
Luigi,

Using the code you provided, I created a performance test to measure the execution time consumed by each process. For a more comprehensive comparison, different numbers of calls were made to both the Harbour and C processes, and the execution time for each was recorded.

The results are attached below.

Speed comparison: lw_NormalizeAngle vs LW_NORMDEG360
======================================
Process calls: 100,000

Time Harbour: 0.05300000 seconds
Time C: 0.03700000 seconds

Relation C/Harbour: 1.43 times faster
Press any key to continue...

Speed comparison: lw_NormalizeAngle vs LW_NORMDEG360
======================================
Process calls: 1,000,000

Time Harbour: 0.50600000 seconds
Time C: 0.36000000 seconds

Relation C/Harbour: 1.41 times faster
Press any key to continue...

Speed comparison: lw_NormalizeAngle vs LW_NORMDEG360
======================================
Process calls: 10,000,000

Time Harbour: 4.91200000 seconds
Time C: 3.54500000 seconds

Relation C/Harbour: 1.39 times faster
Press any key to continue...

Speed comparison: lw_NormalizeAngle vs LW_NORMDEG360
======================================
Process calls: 100,000,000

Time Harbour: 48.63400000 seconds
Time C: 35.42000000 seconds

Relation C/Harbour: 1.37 times faster
Press any key to continue...

Speed comparison: lw_NormalizeAngle vs LW_NORMDEG360
======================================
Process calls: 500,000,000

Time Harbour: 249.18900000 seconds
Time C: 180.09000000 seconds

Relation C/Harbour: 1.38 times faster
Press any key to continue...

Best regards,

Marcos Jarrin

Luigi Ferraris

unread,
Sep 25, 2025, 4:41:16 AM (yesterday) Sep 25
to harbou...@googlegroups.com
Hi to everyone and especially to Marcos,
many, many thanks for these infos.
Very interesting comparison and as You point out the differences are
minimal.
As I wrote, I'm using many times but related to other functions and not
in a continuous loop.
On the other hand I'm not a C programmer so I can continue to use XBase
code, it's more familiar to me :-).

Best regards,
Luigi
Reply all
Reply to author
Forward
0 new messages