#1 2024-03-01 08:16:20

vster
Member
Registered: 2023-07-10
Posts: 17

FPC call c library speed very slow on aarch64 linux

The efficiency of FPC calling simple C libraries is very low.
My test code is as follows. You would say that the execution time should be very fast, even a few microseconds is enough,
But the reality is 35 microseconds.

1、c libray code,save as forloop.c

     //----------------------------------------------------------------------
     #include <stdint.h>
     #include <time.h>
     #include <stdio.h>
   
     void for_loop_time()
     {
         int i, tmp = 0;
         clock_t t1 = clock();

         for (i = 0; i < 1024; i++)
         {
             tmp += i;
         }
       
         printf("space %d clock_t value %d \n", (int)(clock() - t1), tmp); //print tmp to prohibit gcc optimize
     }
     //-------------------------------------------------------------------------

    use gcc compile, output forloop.o
    gcc -c ./forloop.c -O3

2、in lazarus,create simple program, static link forloop.o
   
    //----------------------------------------
    program testforloop;
   
    {$link ./forloop.o}
    {$linklib c}
   
    uses
       CTypes, sysutils;

    procedure for_loop_time; cdecl; external;

    begin
        for_loop_time;
    end.   
    //----------------------------------------
   
   run this fpc program, printed "35 clock_t" in my aarch64 linux , glibc version 2.28

Offline

#2 2024-03-01 11:59:53

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,567
Website

Re: FPC call c library speed very slow on aarch64 linux

I doubt what is being measured - 1024 loop is too small.
I increase loop to be measured

for (i = 0; i < 1024*1024*1024; i++)

and create 2 program - one what call library from c, and one - from pascal (as you provide) - the results is the same

$ ./forloop_c.out
space 129340 clock_t value -536870912 

$ ./forloop_pas
space 129318 clock_t value -536870912

Offline

#3 2024-03-01 15:31:38

vster
Member
Registered: 2023-07-10
Posts: 17

Re: FPC call c library speed very slow on aarch64 linux

thanks, @mpv,  i generated an ASM file (- al - sh - st) using Lazarus. I have almost forgotten about the assembly,  sad and sad Please help me analyze and see what factors affect efficiency? 
There will indeed be issues on my machine as mentioned by subject top.
but uploading files on the forum seems to be prohibited?
my zip attachment inlcude:  asm file  + c library file + lpr file.
now, c library code is :

//----------------------------------
#include <stdint.h>
#include <time.h>
#include <stdio.h>

static const int8_t cabac_context_init_I[1024][2] = {}; //content not important

void for_loop_time()
{
    int ii, tmp, pre;

    clock_t t1 = clock();

    /* calculate pre-state */
    for( ii= 0; ii < 1024; ii++ ) {
     pre = 2*(((cabac_context_init_I[ii][0] * 35) >>4 ) + cabac_context_init_I[ii][1]) - 127;

        pre^= pre>>31;
        if(pre > 124)
            pre= 124 + (pre&1);

        tmp =  pre;
    }

    printf("for_loop_time space %d %d\n", (int)(clock() - t1), tmp);
}

Offline

Board footer

Powered by FluxBB