Recent

Author Topic: FPC call c library speed very slow  (Read 2766 times)

cwlyj001

  • New Member
  • *
  • Posts: 23
FPC call c library speed very slow
« on: March 01, 2024, 09:21:33 am »
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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Re: FPC call c library speed very slow
« Reply #1 on: March 01, 2024, 09:42:12 am »
Just use -al and look at the assembler. You'll see it is mostly a simple (r)call. so something else goes wrong. Some delayed initialization of glibc on the first clock() maybe?


Similarly you can compare if all the C code is the same by checking the assembler after compiling with -S.  Make sure your tests isn't constructed wrongly and it is optimized to one constant value or so
« Last Edit: March 02, 2024, 11:48:30 am by marcov »

TRon

  • Hero Member
  • *****
  • Posts: 3643
Re: FPC call c library speed very slow
« Reply #2 on: March 01, 2024, 09:55:55 am »
Besides making such claims, try and run the example directly from c first... then you'll know who to blame (hint: it isn't fpc  :D )
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

cdbc

  • Hero Member
  • *****
  • Posts: 1665
    • http://www.cdbc.dk
Re: FPC call c library speed very slow
« Reply #3 on: March 01, 2024, 09:59:52 am »
Hi
Quote
The efficiency of FPC calling simple C libraries is very low.
What a load of bullocks! I use libraries all the time and it goes like stink...
You are doing it wrong!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3643
Re: FPC call c library speed very slow
« Reply #4 on: March 01, 2024, 10:09:07 am »
Standalone c version:
Code: [Select]
space 3 clock_t value 523776

c object invoked from FPC (and no, that isn't a copy-paste error):
Code: [Select]
space 3 clock_t value 523776

Indeed FPC is slower by a landslide...   %)

I wonder what went wrong for you cwlyj001
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: FPC call c library speed very slow
« Reply #5 on: March 01, 2024, 10:58:52 am »
That is because FPC generates almost the same assembler as GNU C does for this type of calls....
So the speed is the same. Speed loss must come from something else...
Can not reproduce.
If I smell bad code it usually is bad code and that includes my own code.

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: FPC call c library speed very slow
« Reply #6 on: March 01, 2024, 01:54:13 pm »
The efficiency of FPC calling simple C libraries is very low.

The timing calculation and output sits entirely inside the C function, so the value printed should not be affected at all by how it is called - so TRon's observed results should not e a surprise.  Unless I completely misunderstand what you are actually testing, looking at the printed result currently is not useful to determine the call overhead.  If you really want to test the call overhead, consider instrumenting the actual call in both a simple C and Pascal program:
Code: Pascal  [Select][+][-]
  1. program testforloop;
  2.  
  3. {$link ./forloop.o}
  4. {$linklib c}
  5.  
  6. procedure for_loop_time; cdecl; external;
  7.  
  8. var
  9.   tstart, tend: QWord;
  10.  
  11. begin
  12.   tstart := GetTickCount64;
  13.   for_loop_time;
  14.   tend := GetTickCount64;
  15.   writeln('Call duration = ', tend - tstart, ' ms');
  16. end.
Consider this a simple (untested) example of how to approach the problem, getting accurate performance measurements require a bit more effort than this. E.g. this will calculate the total time for the call + the procedure execution itself, calculating the call overhead itself still require subtracting the execution time of the procedure.
« Last Edit: March 01, 2024, 02:01:46 pm by ccrause »

cwlyj001

  • New Member
  • *
  • Posts: 23
Re: FPC call c library speed very slow
« Reply #7 on: March 01, 2024, 04:10:54 pm »
thanks for all. follow @marcov advice, i recompiled forloop.c( gcc -c ./forloop.c -O, Is it necessary?) and generated an ASM file (- al - sh - st) using Lazarus. I have almost forgotten about the assembly,  %) Please @ marcov @ all analyze and see what factors affect efficiency?  There will indeed be issues on my machine as mentioned by subject top.
attachments inlcude all files:  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);
}
« Last Edit: March 01, 2024, 05:03:47 pm by cwlyj001 »

TRon

  • Hero Member
  • *****
  • Posts: 3643
Re: FPC call c library speed very slow
« Reply #8 on: March 01, 2024, 04:40:35 pm »
debug ? Are you perhaps running things through the debugger ?
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

cwlyj001

  • New Member
  • *
  • Posts: 23
Re: FPC call c library speed very slow
« Reply #9 on: March 01, 2024, 04:41:31 pm »
Hi
Quote
The efficiency of FPC calling simple C libraries is very low.
What a load of bullocks! I use libraries all the time and it goes like stink...
You are doing it wrong!
Regards Benny

 :D Seeing your reply, I really wish I had made a mistake and been caught by you. This way, I am free from worries.
Don't you want to say a few more words?

cdbc

  • Hero Member
  • *****
  • Posts: 1665
    • http://www.cdbc.dk
Re: FPC call c library speed very slow
« Reply #10 on: March 01, 2024, 05:17:19 pm »
Hi
Nah, that just about covers it  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: FPC call c library speed very slow
« Reply #11 on: March 01, 2024, 05:55:48 pm »
You could have done the lot in pure pascal
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. const
  4.    cabac_context_init_I:array [0..1023, 0..1] of shortint =
  5. (
  6.     (* 0 - 10 *)
  7.     ( 20, -15 ), (  2, 54 ),  (  3,  74 ), ( 20, -15 ),
  8.     (  2,  54 ), (  3, 74 ),  ( -28,127 ), ( -23, 104 ),
  9.     ( -6,  53 ), ( -1, 54 ),  (  7,  51 ),
  10.  
  11.     (* 11 - 23 unused for I *)
  12.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  13.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  14.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  15.     ( 0, 0 ),
  16.  
  17.     (* 24- 39 *)
  18.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  19.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  20.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  21.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  22.  
  23.     (* 40 - 53 *)
  24.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  25.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  26.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  27.     ( 0, 0 ),    ( 0, 0 ),
  28.  
  29.     (* 54 - 59 *)
  30.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  31.     ( 0, 0 ),    ( 0, 0 ),
  32.  
  33.     (* 60 - 69 *)
  34.     ( 0, 41 ),   ( 0, 63 ),   ( 0, 63 ),     ( 0, 63 ),
  35.     ( -9, 83 ),  ( 4, 86 ),   ( 0, 97 ),     ( -7, 72 ),
  36.     ( 13, 41 ),  ( 3, 62 ),
  37.  
  38.     (* 70 -> 87 *)
  39.     ( 0, 11 ),   ( 1, 55 ),   ( 0, 69 ),     ( -17, 127 ),
  40.     ( -13, 102 ),( 0, 82 ),   ( -7, 74 ),    ( -21, 107 ),
  41.     ( -27, 127 ),( -31, 127 ),( -24, 127 ),  ( -18, 95 ),
  42.     ( -27, 127 ),( -21, 114 ),( -30, 127 ),  ( -17, 123 ),
  43.     ( -12, 115 ),( -16, 122 ),
  44.  
  45.     (* 88 -> 104 *)
  46.     ( -11, 115 ),( -12, 63 ), ( -2, 68 ),    ( -15, 84 ),
  47.     ( -13, 104 ),( -3, 70 ),  ( -8, 93 ),    ( -10, 90 ),
  48.     ( -30, 127 ),( -1, 74 ),  ( -6, 97 ),    ( -7, 91 ),
  49.     ( -20, 127 ),( -4, 56 ),  ( -5, 82 ),    ( -7, 76 ),
  50.     ( -22, 125 ),
  51.  
  52.     (* 105 -> 135 *)
  53.     ( -7, 93 ),  ( -11, 87 ), ( -3, 77 ),    ( -5, 71 ),
  54.     ( -4, 63 ),  ( -4, 68 ),  ( -12, 84 ),   ( -7, 62 ),
  55.     ( -7, 65 ),  ( 8, 61 ),   ( 5, 56 ),     ( -2, 66 ),
  56.     ( 1, 64 ),   ( 0, 61 ),   ( -2, 78 ),    ( 1, 50 ),
  57.     ( 7, 52 ),   ( 10, 35 ),  ( 0, 44 ),     ( 11, 38 ),
  58.     ( 1, 45 ),   ( 0, 46 ),   ( 5, 44 ),     ( 31, 17 ),
  59.     ( 1, 51 ),   ( 7, 50 ),   ( 28, 19 ),    ( 16, 33 ),
  60.     ( 14, 62 ),  ( -13, 108 ),( -15, 100 ),
  61.  
  62.     (* 136 -> 165 *)
  63.     ( -13, 101 ),( -13, 91 ), ( -12, 94 ),   ( -10, 88 ),
  64.     ( -16, 84 ), ( -10, 86 ), ( -7, 83 ),    ( -13, 87 ),
  65.     ( -19, 94 ), ( 1, 70 ),   ( 0, 72 ),     ( -5, 74 ),
  66.     ( 18, 59 ),  ( -8, 102 ), ( -15, 100 ),  ( 0, 95 ),
  67.     ( -4, 75 ),  ( 2, 72 ),   ( -11, 75 ),   ( -3, 71 ),
  68.     ( 15, 46 ),  ( -13, 69 ), ( 0, 62 ),     ( 0, 65 ),
  69.     ( 21, 37 ),  ( -15, 72 ), ( 9, 57 ),     ( 16, 54 ),
  70.     ( 0, 62 ),   ( 12, 72 ),
  71.  
  72.     (* 166 -> 196 *)
  73.     ( 24, 0 ),   ( 15, 9 ),   ( 8, 25 ),     ( 13, 18 ),
  74.     ( 15, 9 ),   ( 13, 19 ),  ( 10, 37 ),    ( 12, 18 ),
  75.     ( 6, 29 ),   ( 20, 33 ),  ( 15, 30 ),    ( 4, 45 ),
  76.     ( 1, 58 ),   ( 0, 62 ),   ( 7, 61 ),     ( 12, 38 ),
  77.     ( 11, 45 ),  ( 15, 39 ),  ( 11, 42 ),    ( 13, 44 ),
  78.     ( 16, 45 ),  ( 12, 41 ),  ( 10, 49 ),    ( 30, 34 ),
  79.     ( 18, 42 ),  ( 10, 55 ),  ( 17, 51 ),    ( 17, 46 ),
  80.     ( 0, 89 ),   ( 26, -19 ), ( 22, -17 ),
  81.  
  82.     (* 197 -> 226 *)
  83.     ( 26, -17 ), ( 30, -25 ), ( 28, -20 ),   ( 33, -23 ),
  84.     ( 37, -27 ), ( 33, -23 ), ( 40, -28 ),   ( 38, -17 ),
  85.     ( 33, -11 ), ( 40, -15 ), ( 41, -6 ),    ( 38, 1 ),
  86.     ( 41, 17 ),  ( 30, -6 ),  ( 27, 3 ),     ( 26, 22 ),
  87.     ( 37, -16 ), ( 35, -4 ),  ( 38, -8 ),    ( 38, -3 ),
  88.     ( 37, 3 ),   ( 38, 5 ),   ( 42, 0 ),     ( 35, 16 ),
  89.     ( 39, 22 ),  ( 14, 48 ),  ( 27, 37 ),    ( 21, 60 ),
  90.     ( 12, 68 ),  ( 2, 97 ),
  91.  
  92.     (* 227 -> 251 *)
  93.     ( -3, 71 ),  ( -6, 42 ),  ( -5, 50 ),    ( -3, 54 ),
  94.     ( -2, 62 ),  ( 0, 58 ),   ( 1, 63 ),     ( -2, 72 ),
  95.     ( -1, 74 ),  ( -9, 91 ),  ( -5, 67 ),    ( -5, 27 ),
  96.     ( -3, 39 ),  ( -2, 44 ),  ( 0, 46 ),     ( -16, 64 ),
  97.     ( -8, 68 ),  ( -10, 78 ), ( -6, 77 ),    ( -10, 86 ),
  98.     ( -12, 92 ), ( -15, 55 ), ( -10, 60 ),   ( -6, 62 ),
  99.     ( -4, 65 ),
  100.  
  101.     (* 252 -> 275 *)
  102.     ( -12, 73 ), ( -8, 76 ),  ( -7, 80 ),    ( -9, 88 ),
  103.     ( -17, 110 ),( -11, 97 ), ( -20, 84 ),   ( -11, 79 ),
  104.     ( -6, 73 ),  ( -4, 74 ),  ( -13, 86 ),   ( -13, 96 ),
  105.     ( -11, 97 ), ( -19, 117 ),( -8, 78 ),    ( -5, 33 ),
  106.     ( -4, 48 ),  ( -2, 53 ),  ( -3, 62 ),    ( -13, 71 ),
  107.     ( -10, 79 ), ( -12, 86 ), ( -13, 90 ),   ( -14, 97 ),
  108.  
  109.     (* 276 a bit special (not used, bypass is used instead) *)
  110.     ( 0, 0 ),
  111.  
  112.     (* 277 -> 307 *)
  113.     ( -6, 93 ),  ( -6, 84 ),  ( -8, 79 ),    ( 0, 66 ),
  114.     ( -1, 71 ),  ( 0, 62 ),   ( -2, 60 ),    ( -2, 59 ),
  115.     ( -5, 75 ),  ( -3, 62 ),  ( -4, 58 ),    ( -9, 66 ),
  116.     ( -1, 79 ),  ( 0, 71 ),   ( 3, 68 ),     ( 10, 44 ),
  117.     ( -7, 62 ),  ( 15, 36 ),  ( 14, 40 ),    ( 16, 27 ),
  118.     ( 12, 29 ),  ( 1, 44 ),   ( 20, 36 ),    ( 18, 32 ),
  119.     ( 5, 42 ),   ( 1, 48 ),   ( 10, 62 ),    ( 17, 46 ),
  120.     ( 9, 64 ),   ( -12, 104 ),( -11, 97 ),
  121.  
  122.     (* 308 -> 337 *)
  123.     ( -16, 96 ), ( -7, 88 ),  ( -8, 85 ),    ( -7, 85 ),
  124.     ( -9, 85 ),  ( -13, 88 ), ( 4, 66 ),     ( -3, 77 ),
  125.     ( -3, 76 ),  ( -6, 76 ),  ( 10, 58 ),    ( -1, 76 ),
  126.     ( -1, 83 ),  ( -7, 99 ),  ( -14, 95 ),   ( 2, 95 ),
  127.     ( 0, 76 ),   ( -5, 74 ),  ( 0, 70 ),     ( -11, 75 ),
  128.     ( 1, 68 ),   ( 0, 65 ),   ( -14, 73 ),   ( 3, 62 ),
  129.     ( 4, 62 ),   ( -1, 68 ),  ( -13, 75 ),   ( 11, 55 ),
  130.     ( 5, 64 ),   ( 12, 70 ),
  131.  
  132.     (* 338 -> 368 *)
  133.     ( 15, 6 ),   ( 6, 19 ),   ( 7, 16 ),     ( 12, 14 ),
  134.     ( 18, 13 ),  ( 13, 11 ),  ( 13, 15 ),    ( 15, 16 ),
  135.     ( 12, 23 ),  ( 13, 23 ),  ( 15, 20 ),    ( 14, 26 ),
  136.     ( 14, 44 ),  ( 17, 40 ),  ( 17, 47 ),    ( 24, 17 ),
  137.     ( 21, 21 ),  ( 25, 22 ),  ( 31, 27 ),    ( 22, 29 ),
  138.     ( 19, 35 ),  ( 14, 50 ),  ( 10, 57 ),    ( 7, 63 ),
  139.     ( -2, 77 ),  ( -4, 82 ),  ( -3, 94 ),    ( 9, 69 ),
  140.     ( -12, 109 ),( 36, -35 ), ( 36, -34 ),
  141.  
  142.     (* 369 -> 398 *)
  143.     ( 32, -26 ), ( 37, -30 ), ( 44, -32 ),   ( 34, -18 ),
  144.     ( 34, -15 ), ( 40, -15 ), ( 33, -7 ),    ( 35, -5 ),
  145.     ( 33, 0 ),   ( 38, 2 ),   ( 33, 13 ),    ( 23, 35 ),
  146.     ( 13, 58 ),  ( 29, -3 ),  ( 26, 0 ),     ( 22, 30 ),
  147.     ( 31, -7 ),  ( 35, -15 ), ( 34, -3 ),    ( 34, 3 ),
  148.     ( 36, -1 ),  ( 34, 5 ),   ( 32, 11 ),    ( 35, 5 ),
  149.     ( 34, 12 ),  ( 39, 11 ),  ( 30, 29 ),    ( 34, 26 ),
  150.     ( 29, 39 ),  ( 19, 66 ),
  151.  
  152.     (* 399 -> 435 *)
  153.     (  31,  21 ), (  31,  31 ), (  25,  50 ),
  154.     ( -17, 120 ), ( -20, 112 ), ( -18, 114 ), ( -11,  85 ),
  155.     ( -15,  92 ), ( -14,  89 ), ( -26,  71 ), ( -15,  81 ),
  156.     ( -14,  80 ), (   0,  68 ), ( -14,  70 ), ( -24,  56 ),
  157.     ( -23,  68 ), ( -24,  50 ), ( -11,  74 ), (  23, -13 ),
  158.     (  26, -13 ), (  40, -15 ), (  49, -14 ), (  44,   3 ),
  159.     (  45,   6 ), (  44,  34 ), (  33,  54 ), (  19,  82 ),
  160.     (  -3,  75 ), (  -1,  23 ), (   1,  34 ), (   1,  43 ),
  161.     (   0,  54 ), (  -2,  55 ), (   0,  61 ), (   1,  64 ),
  162.     (   0,  68 ), (  -9,  92 ),
  163.  
  164.     (* 436 -> 459 *)
  165.     ( -14, 106 ), ( -13,  97 ), ( -15,  90 ), ( -12,  90 ),
  166.     ( -18,  88 ), ( -10,  73 ), (  -9,  79 ), ( -14,  86 ),
  167.     ( -10,  73 ), ( -10,  70 ), ( -10,  69 ), (  -5,  66 ),
  168.     (  -9,  64 ), (  -5,  58 ), (   2,  59 ), (  21, -10 ),
  169.     (  24, -11 ), (  28,  -8 ), (  28,  -1 ), (  29,   3 ),
  170.     (  29,   9 ), (  35,  20 ), (  29,  36 ), (  14,  67 ),
  171.  
  172.     (* 460 -> 1024 *)
  173.     ( -17, 123 ), ( -12, 115 ), ( -16, 122 ), ( -11, 115 ),
  174.     ( -12,  63 ), (  -2,  68 ), ( -15,  84 ), ( -13, 104 ),
  175.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  176.     ( -17, 123 ), ( -12, 115 ), ( -16, 122 ), ( -11, 115 ),
  177.     ( -12,  63 ), (  -2,  68 ), ( -15,  84 ), ( -13, 104 ),
  178.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  179.     (  -7,  93 ), ( -11,  87 ), (  -3,  77 ), (  -5,  71 ),
  180.     (  -4,  63 ), (  -4,  68 ), ( -12,  84 ), (  -7,  62 ),
  181.     (  -7,  65 ), (   8,  61 ), (   5,  56 ), (  -2,  66 ),
  182.     (   1,  64 ), (   0,  61 ), (  -2,  78 ), (   1,  50 ),
  183.     (   7,  52 ), (  10,  35 ), (   0,  44 ), (  11,  38 ),
  184.     (   1,  45 ), (   0,  46 ), (   5,  44 ), (  31,  17 ),
  185.     (   1,  51 ), (   7,  50 ), (  28,  19 ), (  16,  33 ),
  186.     (  14,  62 ), ( -13, 108 ), ( -15, 100 ), ( -13, 101 ),
  187.     ( -13,  91 ), ( -12,  94 ), ( -10,  88 ), ( -16,  84 ),
  188.     ( -10,  86 ), (  -7,  83 ), ( -13,  87 ), ( -19,  94 ),
  189.     (   1,  70 ), (   0,  72 ), (  -5,  74 ), (  18,  59 ),
  190.     (  -7,  93 ), ( -11,  87 ), (  -3,  77 ), (  -5,  71 ),
  191.     (  -4,  63 ), (  -4,  68 ), ( -12,  84 ), (  -7,  62 ),
  192.     (  -7,  65 ), (   8,  61 ), (   5,  56 ), (  -2,  66 ),
  193.     (   1,  64 ), (   0,  61 ), (  -2,  78 ), (   1,  50 ),
  194.     (   7,  52 ), (  10,  35 ), (   0,  44 ), (  11,  38 ),
  195.     (   1,  45 ), (   0,  46 ), (   5,  44 ), (  31,  17 ),
  196.     (   1,  51 ), (   7,  50 ), (  28,  19 ), (  16,  33 ),
  197.     (  14,  62 ), ( -13, 108 ), ( -15, 100 ), ( -13, 101 ),
  198.     ( -13,  91 ), ( -12,  94 ), ( -10,  88 ), ( -16,  84 ),
  199.     ( -10,  86 ), (  -7,  83 ), ( -13,  87 ), ( -19,  94 ),
  200.     (   1,  70 ), (   0,  72 ), (  -5,  74 ), (  18,  59 ),
  201.     (  24,   0 ), (  15,   9 ), (   8,  25 ), (  13,  18 ),
  202.     (  15,   9 ), (  13,  19 ), (  10,  37 ), (  12,  18 ),
  203.     (   6,  29 ), (  20,  33 ), (  15,  30 ), (   4,  45 ),
  204.     (   1,  58 ), (   0,  62 ), (   7,  61 ), (  12,  38 ),
  205.     (  11,  45 ), (  15,  39 ), (  11,  42 ), (  13,  44 ),
  206.     (  16,  45 ), (  12,  41 ), (  10,  49 ), (  30,  34 ),
  207.     (  18,  42 ), (  10,  55 ), (  17,  51 ), (  17,  46 ),
  208.     (   0,  89 ), (  26, -19 ), (  22, -17 ), (  26, -17 ),
  209.     (  30, -25 ), (  28, -20 ), (  33, -23 ), (  37, -27 ),
  210.     (  33, -23 ), (  40, -28 ), (  38, -17 ), (  33, -11 ),
  211.     (  40, -15 ), (  41,  -6 ), (  38,   1 ), (  41,  17 ),
  212.     (  24,   0 ), (  15,   9 ), (   8,  25 ), (  13,  18 ),
  213.     (  15,   9 ), (  13,  19 ), (  10,  37 ), (  12,  18 ),
  214.     (   6,  29 ), (  20,  33 ), (  15,  30 ), (   4,  45 ),
  215.     (   1,  58 ), (   0,  62 ), (   7,  61 ), (  12,  38 ),
  216.     (  11,  45 ), (  15,  39 ), (  11,  42 ), (  13,  44 ),
  217.     (  16,  45 ), (  12,  41 ), (  10,  49 ), (  30,  34 ),
  218.     (  18,  42 ), (  10,  55 ), (  17,  51 ), (  17,  46 ),
  219.     (   0,  89 ), (  26, -19 ), (  22, -17 ), (  26, -17 ),
  220.     (  30, -25 ), (  28, -20 ), (  33, -23 ), (  37, -27 ),
  221.     (  33, -23 ), (  40, -28 ), (  38, -17 ), (  33, -11 ),
  222.     (  40, -15 ), (  41,  -6 ), (  38,   1 ), (  41,  17 ),
  223.     ( -17, 120 ), ( -20, 112 ), ( -18, 114 ), ( -11,  85 ),
  224.     ( -15,  92 ), ( -14,  89 ), ( -26,  71 ), ( -15,  81 ),
  225.     ( -14,  80 ), (   0,  68 ), ( -14,  70 ), ( -24,  56 ),
  226.     ( -23,  68 ), ( -24,  50 ), ( -11,  74 ), ( -14, 106 ),
  227.     ( -13,  97 ), ( -15,  90 ), ( -12,  90 ), ( -18,  88 ),
  228.     ( -10,  73 ), (  -9,  79 ), ( -14,  86 ), ( -10,  73 ),
  229.     ( -10,  70 ), ( -10,  69 ), (  -5,  66 ), (  -9,  64 ),
  230.     (  -5,  58 ), (   2,  59 ), (  23, -13 ), (  26, -13 ),
  231.     (  40, -15 ), (  49, -14 ), (  44,   3 ), (  45,   6 ),
  232.     (  44,  34 ), (  33,  54 ), (  19,  82 ), (  21, -10 ),
  233.     (  24, -11 ), (  28,  -8 ), (  28,  -1 ), (  29,   3 ),
  234.     (  29,   9 ), (  35,  20 ), (  29,  36 ), (  14,  67 ),
  235.     (  -3,  75 ), (  -1,  23 ), (   1,  34 ), (   1,  43 ),
  236.     (   0,  54 ), (  -2,  55 ), (   0,  61 ), (   1,  64 ),
  237.     (   0,  68 ), (  -9,  92 ), ( -17, 120 ), ( -20, 112 ),
  238.     ( -18, 114 ), ( -11,  85 ), ( -15,  92 ), ( -14,  89 ),
  239.     ( -26,  71 ), ( -15,  81 ), ( -14,  80 ), (   0,  68 ),
  240.     ( -14,  70 ), ( -24,  56 ), ( -23,  68 ), ( -24,  50 ),
  241.     ( -11,  74 ), ( -14, 106 ), ( -13,  97 ), ( -15,  90 ),
  242.     ( -12,  90 ), ( -18,  88 ), ( -10,  73 ), (  -9,  79 ),
  243.     ( -14,  86 ), ( -10,  73 ), ( -10,  70 ), ( -10,  69 ),
  244.     (  -5,  66 ), (  -9,  64 ), (  -5,  58 ), (   2,  59 ),
  245.     (  23, -13 ), (  26, -13 ), (  40, -15 ), (  49, -14 ),
  246.     (  44,   3 ), (  45,   6 ), (  44,  34 ), (  33,  54 ),
  247.     (  19,  82 ), (  21, -10 ), (  24, -11 ), (  28,  -8 ),
  248.     (  28,  -1 ), (  29,   3 ), (  29,   9 ), (  35,  20 ),
  249.     (  29,  36 ), (  14,  67 ), (  -3,  75 ), (  -1,  23 ),
  250.     (   1,  34 ), (   1,  43 ), (   0,  54 ), (  -2,  55 ),
  251.     (   0,  61 ), (   1,  64 ), (   0,  68 ), (  -9,  92 ),
  252.     (  -6,  93 ), (  -6,  84 ), (  -8,  79 ), (   0,  66 ),
  253.     (  -1,  71 ), (   0,  62 ), (  -2,  60 ), (  -2,  59 ),
  254.     (  -5,  75 ), (  -3,  62 ), (  -4,  58 ), (  -9,  66 ),
  255.     (  -1,  79 ), (   0,  71 ), (   3,  68 ), (  10,  44 ),
  256.     (  -7,  62 ), (  15,  36 ), (  14,  40 ), (  16,  27 ),
  257.     (  12,  29 ), (   1,  44 ), (  20,  36 ), (  18,  32 ),
  258.     (   5,  42 ), (   1,  48 ), (  10,  62 ), (  17,  46 ),
  259.     (   9,  64 ), ( -12, 104 ), ( -11,  97 ), ( -16,  96 ),
  260.     (  -7,  88 ), (  -8,  85 ), (  -7,  85 ), (  -9,  85 ),
  261.     ( -13,  88 ), (   4,  66 ), (  -3,  77 ), (  -3,  76 ),
  262.     (  -6,  76 ), (  10,  58 ), (  -1,  76 ), (  -1,  83 ),
  263.     (  -6,  93 ), (  -6,  84 ), (  -8,  79 ), (   0,  66 ),
  264.     (  -1,  71 ), (   0,  62 ), (  -2,  60 ), (  -2,  59 ),
  265.     (  -5,  75 ), (  -3,  62 ), (  -4,  58 ), (  -9,  66 ),
  266.     (  -1,  79 ), (   0,  71 ), (   3,  68 ), (  10,  44 ),
  267.     (  -7,  62 ), (  15,  36 ), (  14,  40 ), (  16,  27 ),
  268.     (  12,  29 ), (   1,  44 ), (  20,  36 ), (  18,  32 ),
  269.     (   5,  42 ), (   1,  48 ), (  10,  62 ), (  17,  46 ),
  270.     (   9,  64 ), ( -12, 104 ), ( -11,  97 ), ( -16,  96 ),
  271.     (  -7,  88 ), (  -8,  85 ), (  -7,  85 ), (  -9,  85 ),
  272.     ( -13,  88 ), (   4,  66 ), (  -3,  77 ), (  -3,  76 ),
  273.     (  -6,  76 ), (  10,  58 ), (  -1,  76 ), (  -1,  83 ),
  274.     (  15,   6 ), (   6,  19 ), (   7,  16 ), (  12,  14 ),
  275.     (  18,  13 ), (  13,  11 ), (  13,  15 ), (  15,  16 ),
  276.     (  12,  23 ), (  13,  23 ), (  15,  20 ), (  14,  26 ),
  277.     (  14,  44 ), (  17,  40 ), (  17,  47 ), (  24,  17 ),
  278.     (  21,  21 ), (  25,  22 ), (  31,  27 ), (  22,  29 ),
  279.     (  19,  35 ), (  14,  50 ), (  10,  57 ), (   7,  63 ),
  280.     (  -2,  77 ), (  -4,  82 ), (  -3,  94 ), (   9,  69 ),
  281.     ( -12, 109 ), (  36, -35 ), (  36, -34 ), (  32, -26 ),
  282.     (  37, -30 ), (  44, -32 ), (  34, -18 ), (  34, -15 ),
  283.     (  40, -15 ), (  33,  -7 ), (  35,  -5 ), (  33,   0 ),
  284.     (  38,   2 ), (  33,  13 ), (  23,  35 ), (  13,  58 ),
  285.     (  15,   6 ), (   6,  19 ), (   7,  16 ), (  12,  14 ),
  286.     (  18,  13 ), (  13,  11 ), (  13,  15 ), (  15,  16 ),
  287.     (  12,  23 ), (  13,  23 ), (  15,  20 ), (  14,  26 ),
  288.     (  14,  44 ), (  17,  40 ), (  17,  47 ), (  24,  17 ),
  289.     (  21,  21 ), (  25,  22 ), (  31,  27 ), (  22,  29 ),
  290.     (  19,  35 ), (  14,  50 ), (  10,  57 ), (   7,  63 ),
  291.     (  -2,  77 ), (  -4,  82 ), (  -3,  94 ), (   9,  69 ),
  292.     ( -12, 109 ), (  36, -35 ), (  36, -34 ), (  32, -26 ),
  293.     (  37, -30 ), (  44, -32 ), (  34, -18 ), (  34, -15 ),
  294.     (  40, -15 ), (  33,  -7 ), (  35,  -5 ), (  33,   0 ),
  295.     (  38,   2 ), (  33,  13 ), (  23,  35 ), (  13,  58 ),
  296.     (  -3,  71 ), (  -6,  42 ), (  -5,  50 ), (  -3,  54 ),
  297.     (  -2,  62 ), (   0,  58 ), (   1,  63 ), (  -2,  72 ),
  298.     (  -1,  74 ), (  -9,  91 ), (  -5,  67 ), (  -5,  27 ),
  299.     (  -3,  39 ), (  -2,  44 ), (   0,  46 ), ( -16,  64 ),
  300.     (  -8,  68 ), ( -10,  78 ), (  -6,  77 ), ( -10,  86 ),
  301.     ( -12,  92 ), ( -15,  55 ), ( -10,  60 ), (  -6,  62 ),
  302.     (  -4,  65 ), ( -12,  73 ), (  -8,  76 ), (  -7,  80 ),
  303.     (  -9,  88 ), ( -17, 110 ), (  -3,  71 ), (  -6,  42 ),
  304.     (  -5,  50 ), (  -3,  54 ), (  -2,  62 ), (   0,  58 ),
  305.     (   1,  63 ), (  -2,  72 ), (  -1,  74 ), (  -9,  91 ),
  306.     (  -5,  67 ), (  -5,  27 ), (  -3,  39 ), (  -2,  44 ),
  307.     (   0,  46 ), ( -16,  64 ), (  -8,  68 ), ( -10,  78 ),
  308.     (  -6,  77 ), ( -10,  86 ), ( -12,  92 ), ( -15,  55 ),
  309.     ( -10,  60 ), (  -6,  62 ), (  -4,  65 ), ( -12,  73 ),
  310.     (  -8,  76 ), (  -7,  80 ), (  -9,  88 ), ( -17, 110 ),
  311.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  312.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  313.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 )
  314. );
  315. procedure for_loop_time;
  316. var
  317.  i,tmp,pre:integer;
  318.  tl:qword;
  319. begin
  320.  tl :=GetTickCount64;
  321.  for i:=0 to 1023 do
  322.  begin
  323.    pre:= 2*(((cabac_context_init_I[i][0] * 35) >> 4 ) + cabac_context_init_I[i][1]) - 127;
  324.    pre:=pre xor pre >> 31;
  325.    if pre >124 then pre:=124+(pre and 1);
  326.    tmp:=pre;
  327.  end;
  328.  writeln('for_loop_time space ', gettickcount64-tl, tmp:4);
  329. end;
  330. begin
  331.   for_loop_time;
  332. end.
If I smell bad code it usually is bad code and that includes my own code.

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: FPC call c library speed very slow
« Reply #12 on: March 01, 2024, 06:34:58 pm »
thanks for all. follow @marcov advice, i recompiled forloop.c( gcc -c ./forloop.c -O, Is it necessary?) and generated an ASM file (- al - sh - st) using Lazarus. I have almost forgotten about the assembly,  %) Please @ marcov @ all analyze and see what factors affect efficiency?  There will indeed be issues on my machine as mentioned by subject top.

What are you measuring?  The only measurement I see in your code is inside your C procedure, and that measures the time taken by the C code loop only.

Anyway, here is the sum total of the assembly instructions used to call the C procedure according to your assembly listing (are you testing this on a RPi?):
Code: ASM  [Select][+][-]
  1. // [12] for_loop_time;
  2.         bl      for_loop_time
  3. .Ll3:
  4. // [13] end.
A single branch & link instruction, not much to improve on here.

cwlyj001

  • New Member
  • *
  • Posts: 23
Re: FPC call c library speed very slow
« Reply #13 on: March 02, 2024, 04:17:43 am »
You could have done the lot in pure pascal
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses sysutils;
  3. const
  4.    cabac_context_init_I:array [0..1023, 0..1] of shortint =
  5. (
  6.     (* 0 - 10 *)
  7.     ( 20, -15 ), (  2, 54 ),  (  3,  74 ), ( 20, -15 ),
  8.     (  2,  54 ), (  3, 74 ),  ( -28,127 ), ( -23, 104 ),
  9.     ( -6,  53 ), ( -1, 54 ),  (  7,  51 ),
  10.  
  11.     (* 11 - 23 unused for I *)
  12.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  13.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  14.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  15.     ( 0, 0 ),
  16.  
  17.     (* 24- 39 *)
  18.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  19.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  20.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  21.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  22.  
  23.     (* 40 - 53 *)
  24.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  25.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  26.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  27.     ( 0, 0 ),    ( 0, 0 ),
  28.  
  29.     (* 54 - 59 *)
  30.     ( 0, 0 ),    ( 0, 0 ),    ( 0, 0 ),      ( 0, 0 ),
  31.     ( 0, 0 ),    ( 0, 0 ),
  32.  
  33.     (* 60 - 69 *)
  34.     ( 0, 41 ),   ( 0, 63 ),   ( 0, 63 ),     ( 0, 63 ),
  35.     ( -9, 83 ),  ( 4, 86 ),   ( 0, 97 ),     ( -7, 72 ),
  36.     ( 13, 41 ),  ( 3, 62 ),
  37.  
  38.     (* 70 -> 87 *)
  39.     ( 0, 11 ),   ( 1, 55 ),   ( 0, 69 ),     ( -17, 127 ),
  40.     ( -13, 102 ),( 0, 82 ),   ( -7, 74 ),    ( -21, 107 ),
  41.     ( -27, 127 ),( -31, 127 ),( -24, 127 ),  ( -18, 95 ),
  42.     ( -27, 127 ),( -21, 114 ),( -30, 127 ),  ( -17, 123 ),
  43.     ( -12, 115 ),( -16, 122 ),
  44.  
  45.     (* 88 -> 104 *)
  46.     ( -11, 115 ),( -12, 63 ), ( -2, 68 ),    ( -15, 84 ),
  47.     ( -13, 104 ),( -3, 70 ),  ( -8, 93 ),    ( -10, 90 ),
  48.     ( -30, 127 ),( -1, 74 ),  ( -6, 97 ),    ( -7, 91 ),
  49.     ( -20, 127 ),( -4, 56 ),  ( -5, 82 ),    ( -7, 76 ),
  50.     ( -22, 125 ),
  51.  
  52.     (* 105 -> 135 *)
  53.     ( -7, 93 ),  ( -11, 87 ), ( -3, 77 ),    ( -5, 71 ),
  54.     ( -4, 63 ),  ( -4, 68 ),  ( -12, 84 ),   ( -7, 62 ),
  55.     ( -7, 65 ),  ( 8, 61 ),   ( 5, 56 ),     ( -2, 66 ),
  56.     ( 1, 64 ),   ( 0, 61 ),   ( -2, 78 ),    ( 1, 50 ),
  57.     ( 7, 52 ),   ( 10, 35 ),  ( 0, 44 ),     ( 11, 38 ),
  58.     ( 1, 45 ),   ( 0, 46 ),   ( 5, 44 ),     ( 31, 17 ),
  59.     ( 1, 51 ),   ( 7, 50 ),   ( 28, 19 ),    ( 16, 33 ),
  60.     ( 14, 62 ),  ( -13, 108 ),( -15, 100 ),
  61.  
  62.     (* 136 -> 165 *)
  63.     ( -13, 101 ),( -13, 91 ), ( -12, 94 ),   ( -10, 88 ),
  64.     ( -16, 84 ), ( -10, 86 ), ( -7, 83 ),    ( -13, 87 ),
  65.     ( -19, 94 ), ( 1, 70 ),   ( 0, 72 ),     ( -5, 74 ),
  66.     ( 18, 59 ),  ( -8, 102 ), ( -15, 100 ),  ( 0, 95 ),
  67.     ( -4, 75 ),  ( 2, 72 ),   ( -11, 75 ),   ( -3, 71 ),
  68.     ( 15, 46 ),  ( -13, 69 ), ( 0, 62 ),     ( 0, 65 ),
  69.     ( 21, 37 ),  ( -15, 72 ), ( 9, 57 ),     ( 16, 54 ),
  70.     ( 0, 62 ),   ( 12, 72 ),
  71.  
  72.     (* 166 -> 196 *)
  73.     ( 24, 0 ),   ( 15, 9 ),   ( 8, 25 ),     ( 13, 18 ),
  74.     ( 15, 9 ),   ( 13, 19 ),  ( 10, 37 ),    ( 12, 18 ),
  75.     ( 6, 29 ),   ( 20, 33 ),  ( 15, 30 ),    ( 4, 45 ),
  76.     ( 1, 58 ),   ( 0, 62 ),   ( 7, 61 ),     ( 12, 38 ),
  77.     ( 11, 45 ),  ( 15, 39 ),  ( 11, 42 ),    ( 13, 44 ),
  78.     ( 16, 45 ),  ( 12, 41 ),  ( 10, 49 ),    ( 30, 34 ),
  79.     ( 18, 42 ),  ( 10, 55 ),  ( 17, 51 ),    ( 17, 46 ),
  80.     ( 0, 89 ),   ( 26, -19 ), ( 22, -17 ),
  81.  
  82.     (* 197 -> 226 *)
  83.     ( 26, -17 ), ( 30, -25 ), ( 28, -20 ),   ( 33, -23 ),
  84.     ( 37, -27 ), ( 33, -23 ), ( 40, -28 ),   ( 38, -17 ),
  85.     ( 33, -11 ), ( 40, -15 ), ( 41, -6 ),    ( 38, 1 ),
  86.     ( 41, 17 ),  ( 30, -6 ),  ( 27, 3 ),     ( 26, 22 ),
  87.     ( 37, -16 ), ( 35, -4 ),  ( 38, -8 ),    ( 38, -3 ),
  88.     ( 37, 3 ),   ( 38, 5 ),   ( 42, 0 ),     ( 35, 16 ),
  89.     ( 39, 22 ),  ( 14, 48 ),  ( 27, 37 ),    ( 21, 60 ),
  90.     ( 12, 68 ),  ( 2, 97 ),
  91.  
  92.     (* 227 -> 251 *)
  93.     ( -3, 71 ),  ( -6, 42 ),  ( -5, 50 ),    ( -3, 54 ),
  94.     ( -2, 62 ),  ( 0, 58 ),   ( 1, 63 ),     ( -2, 72 ),
  95.     ( -1, 74 ),  ( -9, 91 ),  ( -5, 67 ),    ( -5, 27 ),
  96.     ( -3, 39 ),  ( -2, 44 ),  ( 0, 46 ),     ( -16, 64 ),
  97.     ( -8, 68 ),  ( -10, 78 ), ( -6, 77 ),    ( -10, 86 ),
  98.     ( -12, 92 ), ( -15, 55 ), ( -10, 60 ),   ( -6, 62 ),
  99.     ( -4, 65 ),
  100.  
  101.     (* 252 -> 275 *)
  102.     ( -12, 73 ), ( -8, 76 ),  ( -7, 80 ),    ( -9, 88 ),
  103.     ( -17, 110 ),( -11, 97 ), ( -20, 84 ),   ( -11, 79 ),
  104.     ( -6, 73 ),  ( -4, 74 ),  ( -13, 86 ),   ( -13, 96 ),
  105.     ( -11, 97 ), ( -19, 117 ),( -8, 78 ),    ( -5, 33 ),
  106.     ( -4, 48 ),  ( -2, 53 ),  ( -3, 62 ),    ( -13, 71 ),
  107.     ( -10, 79 ), ( -12, 86 ), ( -13, 90 ),   ( -14, 97 ),
  108.  
  109.     (* 276 a bit special (not used, bypass is used instead) *)
  110.     ( 0, 0 ),
  111.  
  112.     (* 277 -> 307 *)
  113.     ( -6, 93 ),  ( -6, 84 ),  ( -8, 79 ),    ( 0, 66 ),
  114.     ( -1, 71 ),  ( 0, 62 ),   ( -2, 60 ),    ( -2, 59 ),
  115.     ( -5, 75 ),  ( -3, 62 ),  ( -4, 58 ),    ( -9, 66 ),
  116.     ( -1, 79 ),  ( 0, 71 ),   ( 3, 68 ),     ( 10, 44 ),
  117.     ( -7, 62 ),  ( 15, 36 ),  ( 14, 40 ),    ( 16, 27 ),
  118.     ( 12, 29 ),  ( 1, 44 ),   ( 20, 36 ),    ( 18, 32 ),
  119.     ( 5, 42 ),   ( 1, 48 ),   ( 10, 62 ),    ( 17, 46 ),
  120.     ( 9, 64 ),   ( -12, 104 ),( -11, 97 ),
  121.  
  122.     (* 308 -> 337 *)
  123.     ( -16, 96 ), ( -7, 88 ),  ( -8, 85 ),    ( -7, 85 ),
  124.     ( -9, 85 ),  ( -13, 88 ), ( 4, 66 ),     ( -3, 77 ),
  125.     ( -3, 76 ),  ( -6, 76 ),  ( 10, 58 ),    ( -1, 76 ),
  126.     ( -1, 83 ),  ( -7, 99 ),  ( -14, 95 ),   ( 2, 95 ),
  127.     ( 0, 76 ),   ( -5, 74 ),  ( 0, 70 ),     ( -11, 75 ),
  128.     ( 1, 68 ),   ( 0, 65 ),   ( -14, 73 ),   ( 3, 62 ),
  129.     ( 4, 62 ),   ( -1, 68 ),  ( -13, 75 ),   ( 11, 55 ),
  130.     ( 5, 64 ),   ( 12, 70 ),
  131.  
  132.     (* 338 -> 368 *)
  133.     ( 15, 6 ),   ( 6, 19 ),   ( 7, 16 ),     ( 12, 14 ),
  134.     ( 18, 13 ),  ( 13, 11 ),  ( 13, 15 ),    ( 15, 16 ),
  135.     ( 12, 23 ),  ( 13, 23 ),  ( 15, 20 ),    ( 14, 26 ),
  136.     ( 14, 44 ),  ( 17, 40 ),  ( 17, 47 ),    ( 24, 17 ),
  137.     ( 21, 21 ),  ( 25, 22 ),  ( 31, 27 ),    ( 22, 29 ),
  138.     ( 19, 35 ),  ( 14, 50 ),  ( 10, 57 ),    ( 7, 63 ),
  139.     ( -2, 77 ),  ( -4, 82 ),  ( -3, 94 ),    ( 9, 69 ),
  140.     ( -12, 109 ),( 36, -35 ), ( 36, -34 ),
  141.  
  142.     (* 369 -> 398 *)
  143.     ( 32, -26 ), ( 37, -30 ), ( 44, -32 ),   ( 34, -18 ),
  144.     ( 34, -15 ), ( 40, -15 ), ( 33, -7 ),    ( 35, -5 ),
  145.     ( 33, 0 ),   ( 38, 2 ),   ( 33, 13 ),    ( 23, 35 ),
  146.     ( 13, 58 ),  ( 29, -3 ),  ( 26, 0 ),     ( 22, 30 ),
  147.     ( 31, -7 ),  ( 35, -15 ), ( 34, -3 ),    ( 34, 3 ),
  148.     ( 36, -1 ),  ( 34, 5 ),   ( 32, 11 ),    ( 35, 5 ),
  149.     ( 34, 12 ),  ( 39, 11 ),  ( 30, 29 ),    ( 34, 26 ),
  150.     ( 29, 39 ),  ( 19, 66 ),
  151.  
  152.     (* 399 -> 435 *)
  153.     (  31,  21 ), (  31,  31 ), (  25,  50 ),
  154.     ( -17, 120 ), ( -20, 112 ), ( -18, 114 ), ( -11,  85 ),
  155.     ( -15,  92 ), ( -14,  89 ), ( -26,  71 ), ( -15,  81 ),
  156.     ( -14,  80 ), (   0,  68 ), ( -14,  70 ), ( -24,  56 ),
  157.     ( -23,  68 ), ( -24,  50 ), ( -11,  74 ), (  23, -13 ),
  158.     (  26, -13 ), (  40, -15 ), (  49, -14 ), (  44,   3 ),
  159.     (  45,   6 ), (  44,  34 ), (  33,  54 ), (  19,  82 ),
  160.     (  -3,  75 ), (  -1,  23 ), (   1,  34 ), (   1,  43 ),
  161.     (   0,  54 ), (  -2,  55 ), (   0,  61 ), (   1,  64 ),
  162.     (   0,  68 ), (  -9,  92 ),
  163.  
  164.     (* 436 -> 459 *)
  165.     ( -14, 106 ), ( -13,  97 ), ( -15,  90 ), ( -12,  90 ),
  166.     ( -18,  88 ), ( -10,  73 ), (  -9,  79 ), ( -14,  86 ),
  167.     ( -10,  73 ), ( -10,  70 ), ( -10,  69 ), (  -5,  66 ),
  168.     (  -9,  64 ), (  -5,  58 ), (   2,  59 ), (  21, -10 ),
  169.     (  24, -11 ), (  28,  -8 ), (  28,  -1 ), (  29,   3 ),
  170.     (  29,   9 ), (  35,  20 ), (  29,  36 ), (  14,  67 ),
  171.  
  172.     (* 460 -> 1024 *)
  173.     ( -17, 123 ), ( -12, 115 ), ( -16, 122 ), ( -11, 115 ),
  174.     ( -12,  63 ), (  -2,  68 ), ( -15,  84 ), ( -13, 104 ),
  175.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  176.     ( -17, 123 ), ( -12, 115 ), ( -16, 122 ), ( -11, 115 ),
  177.     ( -12,  63 ), (  -2,  68 ), ( -15,  84 ), ( -13, 104 ),
  178.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  179.     (  -7,  93 ), ( -11,  87 ), (  -3,  77 ), (  -5,  71 ),
  180.     (  -4,  63 ), (  -4,  68 ), ( -12,  84 ), (  -7,  62 ),
  181.     (  -7,  65 ), (   8,  61 ), (   5,  56 ), (  -2,  66 ),
  182.     (   1,  64 ), (   0,  61 ), (  -2,  78 ), (   1,  50 ),
  183.     (   7,  52 ), (  10,  35 ), (   0,  44 ), (  11,  38 ),
  184.     (   1,  45 ), (   0,  46 ), (   5,  44 ), (  31,  17 ),
  185.     (   1,  51 ), (   7,  50 ), (  28,  19 ), (  16,  33 ),
  186.     (  14,  62 ), ( -13, 108 ), ( -15, 100 ), ( -13, 101 ),
  187.     ( -13,  91 ), ( -12,  94 ), ( -10,  88 ), ( -16,  84 ),
  188.     ( -10,  86 ), (  -7,  83 ), ( -13,  87 ), ( -19,  94 ),
  189.     (   1,  70 ), (   0,  72 ), (  -5,  74 ), (  18,  59 ),
  190.     (  -7,  93 ), ( -11,  87 ), (  -3,  77 ), (  -5,  71 ),
  191.     (  -4,  63 ), (  -4,  68 ), ( -12,  84 ), (  -7,  62 ),
  192.     (  -7,  65 ), (   8,  61 ), (   5,  56 ), (  -2,  66 ),
  193.     (   1,  64 ), (   0,  61 ), (  -2,  78 ), (   1,  50 ),
  194.     (   7,  52 ), (  10,  35 ), (   0,  44 ), (  11,  38 ),
  195.     (   1,  45 ), (   0,  46 ), (   5,  44 ), (  31,  17 ),
  196.     (   1,  51 ), (   7,  50 ), (  28,  19 ), (  16,  33 ),
  197.     (  14,  62 ), ( -13, 108 ), ( -15, 100 ), ( -13, 101 ),
  198.     ( -13,  91 ), ( -12,  94 ), ( -10,  88 ), ( -16,  84 ),
  199.     ( -10,  86 ), (  -7,  83 ), ( -13,  87 ), ( -19,  94 ),
  200.     (   1,  70 ), (   0,  72 ), (  -5,  74 ), (  18,  59 ),
  201.     (  24,   0 ), (  15,   9 ), (   8,  25 ), (  13,  18 ),
  202.     (  15,   9 ), (  13,  19 ), (  10,  37 ), (  12,  18 ),
  203.     (   6,  29 ), (  20,  33 ), (  15,  30 ), (   4,  45 ),
  204.     (   1,  58 ), (   0,  62 ), (   7,  61 ), (  12,  38 ),
  205.     (  11,  45 ), (  15,  39 ), (  11,  42 ), (  13,  44 ),
  206.     (  16,  45 ), (  12,  41 ), (  10,  49 ), (  30,  34 ),
  207.     (  18,  42 ), (  10,  55 ), (  17,  51 ), (  17,  46 ),
  208.     (   0,  89 ), (  26, -19 ), (  22, -17 ), (  26, -17 ),
  209.     (  30, -25 ), (  28, -20 ), (  33, -23 ), (  37, -27 ),
  210.     (  33, -23 ), (  40, -28 ), (  38, -17 ), (  33, -11 ),
  211.     (  40, -15 ), (  41,  -6 ), (  38,   1 ), (  41,  17 ),
  212.     (  24,   0 ), (  15,   9 ), (   8,  25 ), (  13,  18 ),
  213.     (  15,   9 ), (  13,  19 ), (  10,  37 ), (  12,  18 ),
  214.     (   6,  29 ), (  20,  33 ), (  15,  30 ), (   4,  45 ),
  215.     (   1,  58 ), (   0,  62 ), (   7,  61 ), (  12,  38 ),
  216.     (  11,  45 ), (  15,  39 ), (  11,  42 ), (  13,  44 ),
  217.     (  16,  45 ), (  12,  41 ), (  10,  49 ), (  30,  34 ),
  218.     (  18,  42 ), (  10,  55 ), (  17,  51 ), (  17,  46 ),
  219.     (   0,  89 ), (  26, -19 ), (  22, -17 ), (  26, -17 ),
  220.     (  30, -25 ), (  28, -20 ), (  33, -23 ), (  37, -27 ),
  221.     (  33, -23 ), (  40, -28 ), (  38, -17 ), (  33, -11 ),
  222.     (  40, -15 ), (  41,  -6 ), (  38,   1 ), (  41,  17 ),
  223.     ( -17, 120 ), ( -20, 112 ), ( -18, 114 ), ( -11,  85 ),
  224.     ( -15,  92 ), ( -14,  89 ), ( -26,  71 ), ( -15,  81 ),
  225.     ( -14,  80 ), (   0,  68 ), ( -14,  70 ), ( -24,  56 ),
  226.     ( -23,  68 ), ( -24,  50 ), ( -11,  74 ), ( -14, 106 ),
  227.     ( -13,  97 ), ( -15,  90 ), ( -12,  90 ), ( -18,  88 ),
  228.     ( -10,  73 ), (  -9,  79 ), ( -14,  86 ), ( -10,  73 ),
  229.     ( -10,  70 ), ( -10,  69 ), (  -5,  66 ), (  -9,  64 ),
  230.     (  -5,  58 ), (   2,  59 ), (  23, -13 ), (  26, -13 ),
  231.     (  40, -15 ), (  49, -14 ), (  44,   3 ), (  45,   6 ),
  232.     (  44,  34 ), (  33,  54 ), (  19,  82 ), (  21, -10 ),
  233.     (  24, -11 ), (  28,  -8 ), (  28,  -1 ), (  29,   3 ),
  234.     (  29,   9 ), (  35,  20 ), (  29,  36 ), (  14,  67 ),
  235.     (  -3,  75 ), (  -1,  23 ), (   1,  34 ), (   1,  43 ),
  236.     (   0,  54 ), (  -2,  55 ), (   0,  61 ), (   1,  64 ),
  237.     (   0,  68 ), (  -9,  92 ), ( -17, 120 ), ( -20, 112 ),
  238.     ( -18, 114 ), ( -11,  85 ), ( -15,  92 ), ( -14,  89 ),
  239.     ( -26,  71 ), ( -15,  81 ), ( -14,  80 ), (   0,  68 ),
  240.     ( -14,  70 ), ( -24,  56 ), ( -23,  68 ), ( -24,  50 ),
  241.     ( -11,  74 ), ( -14, 106 ), ( -13,  97 ), ( -15,  90 ),
  242.     ( -12,  90 ), ( -18,  88 ), ( -10,  73 ), (  -9,  79 ),
  243.     ( -14,  86 ), ( -10,  73 ), ( -10,  70 ), ( -10,  69 ),
  244.     (  -5,  66 ), (  -9,  64 ), (  -5,  58 ), (   2,  59 ),
  245.     (  23, -13 ), (  26, -13 ), (  40, -15 ), (  49, -14 ),
  246.     (  44,   3 ), (  45,   6 ), (  44,  34 ), (  33,  54 ),
  247.     (  19,  82 ), (  21, -10 ), (  24, -11 ), (  28,  -8 ),
  248.     (  28,  -1 ), (  29,   3 ), (  29,   9 ), (  35,  20 ),
  249.     (  29,  36 ), (  14,  67 ), (  -3,  75 ), (  -1,  23 ),
  250.     (   1,  34 ), (   1,  43 ), (   0,  54 ), (  -2,  55 ),
  251.     (   0,  61 ), (   1,  64 ), (   0,  68 ), (  -9,  92 ),
  252.     (  -6,  93 ), (  -6,  84 ), (  -8,  79 ), (   0,  66 ),
  253.     (  -1,  71 ), (   0,  62 ), (  -2,  60 ), (  -2,  59 ),
  254.     (  -5,  75 ), (  -3,  62 ), (  -4,  58 ), (  -9,  66 ),
  255.     (  -1,  79 ), (   0,  71 ), (   3,  68 ), (  10,  44 ),
  256.     (  -7,  62 ), (  15,  36 ), (  14,  40 ), (  16,  27 ),
  257.     (  12,  29 ), (   1,  44 ), (  20,  36 ), (  18,  32 ),
  258.     (   5,  42 ), (   1,  48 ), (  10,  62 ), (  17,  46 ),
  259.     (   9,  64 ), ( -12, 104 ), ( -11,  97 ), ( -16,  96 ),
  260.     (  -7,  88 ), (  -8,  85 ), (  -7,  85 ), (  -9,  85 ),
  261.     ( -13,  88 ), (   4,  66 ), (  -3,  77 ), (  -3,  76 ),
  262.     (  -6,  76 ), (  10,  58 ), (  -1,  76 ), (  -1,  83 ),
  263.     (  -6,  93 ), (  -6,  84 ), (  -8,  79 ), (   0,  66 ),
  264.     (  -1,  71 ), (   0,  62 ), (  -2,  60 ), (  -2,  59 ),
  265.     (  -5,  75 ), (  -3,  62 ), (  -4,  58 ), (  -9,  66 ),
  266.     (  -1,  79 ), (   0,  71 ), (   3,  68 ), (  10,  44 ),
  267.     (  -7,  62 ), (  15,  36 ), (  14,  40 ), (  16,  27 ),
  268.     (  12,  29 ), (   1,  44 ), (  20,  36 ), (  18,  32 ),
  269.     (   5,  42 ), (   1,  48 ), (  10,  62 ), (  17,  46 ),
  270.     (   9,  64 ), ( -12, 104 ), ( -11,  97 ), ( -16,  96 ),
  271.     (  -7,  88 ), (  -8,  85 ), (  -7,  85 ), (  -9,  85 ),
  272.     ( -13,  88 ), (   4,  66 ), (  -3,  77 ), (  -3,  76 ),
  273.     (  -6,  76 ), (  10,  58 ), (  -1,  76 ), (  -1,  83 ),
  274.     (  15,   6 ), (   6,  19 ), (   7,  16 ), (  12,  14 ),
  275.     (  18,  13 ), (  13,  11 ), (  13,  15 ), (  15,  16 ),
  276.     (  12,  23 ), (  13,  23 ), (  15,  20 ), (  14,  26 ),
  277.     (  14,  44 ), (  17,  40 ), (  17,  47 ), (  24,  17 ),
  278.     (  21,  21 ), (  25,  22 ), (  31,  27 ), (  22,  29 ),
  279.     (  19,  35 ), (  14,  50 ), (  10,  57 ), (   7,  63 ),
  280.     (  -2,  77 ), (  -4,  82 ), (  -3,  94 ), (   9,  69 ),
  281.     ( -12, 109 ), (  36, -35 ), (  36, -34 ), (  32, -26 ),
  282.     (  37, -30 ), (  44, -32 ), (  34, -18 ), (  34, -15 ),
  283.     (  40, -15 ), (  33,  -7 ), (  35,  -5 ), (  33,   0 ),
  284.     (  38,   2 ), (  33,  13 ), (  23,  35 ), (  13,  58 ),
  285.     (  15,   6 ), (   6,  19 ), (   7,  16 ), (  12,  14 ),
  286.     (  18,  13 ), (  13,  11 ), (  13,  15 ), (  15,  16 ),
  287.     (  12,  23 ), (  13,  23 ), (  15,  20 ), (  14,  26 ),
  288.     (  14,  44 ), (  17,  40 ), (  17,  47 ), (  24,  17 ),
  289.     (  21,  21 ), (  25,  22 ), (  31,  27 ), (  22,  29 ),
  290.     (  19,  35 ), (  14,  50 ), (  10,  57 ), (   7,  63 ),
  291.     (  -2,  77 ), (  -4,  82 ), (  -3,  94 ), (   9,  69 ),
  292.     ( -12, 109 ), (  36, -35 ), (  36, -34 ), (  32, -26 ),
  293.     (  37, -30 ), (  44, -32 ), (  34, -18 ), (  34, -15 ),
  294.     (  40, -15 ), (  33,  -7 ), (  35,  -5 ), (  33,   0 ),
  295.     (  38,   2 ), (  33,  13 ), (  23,  35 ), (  13,  58 ),
  296.     (  -3,  71 ), (  -6,  42 ), (  -5,  50 ), (  -3,  54 ),
  297.     (  -2,  62 ), (   0,  58 ), (   1,  63 ), (  -2,  72 ),
  298.     (  -1,  74 ), (  -9,  91 ), (  -5,  67 ), (  -5,  27 ),
  299.     (  -3,  39 ), (  -2,  44 ), (   0,  46 ), ( -16,  64 ),
  300.     (  -8,  68 ), ( -10,  78 ), (  -6,  77 ), ( -10,  86 ),
  301.     ( -12,  92 ), ( -15,  55 ), ( -10,  60 ), (  -6,  62 ),
  302.     (  -4,  65 ), ( -12,  73 ), (  -8,  76 ), (  -7,  80 ),
  303.     (  -9,  88 ), ( -17, 110 ), (  -3,  71 ), (  -6,  42 ),
  304.     (  -5,  50 ), (  -3,  54 ), (  -2,  62 ), (   0,  58 ),
  305.     (   1,  63 ), (  -2,  72 ), (  -1,  74 ), (  -9,  91 ),
  306.     (  -5,  67 ), (  -5,  27 ), (  -3,  39 ), (  -2,  44 ),
  307.     (   0,  46 ), ( -16,  64 ), (  -8,  68 ), ( -10,  78 ),
  308.     (  -6,  77 ), ( -10,  86 ), ( -12,  92 ), ( -15,  55 ),
  309.     ( -10,  60 ), (  -6,  62 ), (  -4,  65 ), ( -12,  73 ),
  310.     (  -8,  76 ), (  -7,  80 ), (  -9,  88 ), ( -17, 110 ),
  311.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  312.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 ),
  313.     (  -3,  70 ), (  -8,  93 ), ( -10,  90 ), ( -30, 127 )
  314. );
  315. procedure for_loop_time;
  316. var
  317.  i,tmp,pre:integer;
  318.  tl:qword;
  319. begin
  320.  tl :=GetTickCount64;
  321.  for i:=0 to 1023 do
  322.  begin
  323.    pre:= 2*(((cabac_context_init_I[i][0] * 35) >> 4 ) + cabac_context_init_I[i][1]) - 127;
  324.    pre:=pre xor pre >> 31;
  325.    if pre >124 then pre:=124+(pre and 1);
  326.    tmp:=pre;
  327.  end;
  328.  writeln('for_loop_time space ', gettickcount64-tl, tmp:4);
  329. end;
  330. begin
  331.   for_loop_time;
  332. end.

I have also tested pure pascal, which took 0 milliseconds.
But the reality is that I have to call C libraries (such as ffmpeg), which means during the process of calling ffmpeg decoding
Identify effectiveness issues. A simple demo test still has efficiency issues. It's like this.

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: FPC call c library speed very slow
« Reply #14 on: March 02, 2024, 07:13:42 am »
well, nobody else has efficiency issues with the code you provided.
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018