Re: [fpc-pascal] Program efficiency
On 11/9/2022 6:52 AM, James Richters via fpc-pascal wrote: Sounds to me that if the compiler will probably insert temp variables anyway, then I might as well make my own and make it easier to understand later. +1 Trying to write this without intermediate variables would definitely make is less readable and the only case (maybe) where this could possibly, and even then likely in limited cases be more efficient, would be if you have a CPU architecture with MANY 64bit registers. But even 64bit ARM gets limited rather quickly and it certainly won't help on x86, which is still the vastly dominant CPU architecture out there... Ralf ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
On Wed, 9 Nov 2022, geneb via fpc-pascal wrote: On Wed, 9 Nov 2022, DougC via fpc-pascal wrote: Maintaining understandable and readable code is often more important that runtime efficiency. Like others have said, there are likely many additional places you can address overall efficiency before dealing with these calculations. THIS. Clarity over cleverness, every single time. It's the job of the compiler to be clever, so we don't have to be ;-) Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
On Wed, 9 Nov 2022, DougC via fpc-pascal wrote: Maintaining understandable and readable code is often more important that runtime efficiency. Like others have said, there are likely many additional places you can address overall efficiency before dealing with these calculations. THIS. Clarity over cleverness, every single time. g. -- Proud owner of F-15C 80-0007 http://www.f15sim.com - The only one of its kind. http://www.diy-cockpits.org/coll - Go Collimated or Go Home. Some people collect things for a hobby. Geeks collect hobbies. ScarletDME - The red hot Data Management Environment A Multi-Value database for the masses, not the classes. http://scarlet.deltasoft.com - Get it _today_! ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
Maintaining understandable and readable code is often more important that runtime efficiency. Like others have said, there are likely many additional places you can address overall efficiency before dealing with these calculations. Doug C. On Wed, 09 Nov 2022 09:52:12 -0500 James Richters via fpc-pascal wrote --- Sounds to me that if the compiler will probably insert temp variables anyway, then I might as well make my own and make it easier to understand later. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
Sounds to me that if the compiler will probably insert temp variables anyway, then I might as well make my own and make it easier to understand later. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
Sorry, it was copy-paste from github and it broke formatting do { v1 = XXH32_round(v1, XXH_get32bits(input)); input += 4; v2 = XXH32_round(v2, XXH_get32bits(input)); input += 4; v3 = XXH32_round(v3, XXH_get32bits(input)); input += 4; v4 = XXH32_round(v4, XXH_get32bits(input)); input += 4; } while (input < limit); repeat v1 := cPrime32x1 * RolDWord(v1 + cPrime32x2 * PLongWord(ABuffer)^, 13); v2 := cPrime32x1 * RolDWord(v2 + cPrime32x2 * PLongWord(ABuffer+4)^, 13); v3 := cPrime32x1 * RolDWord(v3 + cPrime32x2 * PLongWord(ABuffer+8)^, 13); v4 := cPrime32x1 * RolDWord(v4 + cPrime32x2 * PLongWord(ABuffer+12)^, 13); inc(ABuffer, 16); until not (ABuffer <= pLimit); __ Od: "James Richters via fpc-pascal" Komu: "'FPC-Pascal users discussions'" Datum: 09.11.2022 13:47 Předmět: [fpc-pascal] Program efficiency I was wondering if breaking up large complicated formulas into smaller sections that use more variables is less efficient than just the large formula. In other words.. is A:=(B+C/D)-E^F; G:=H*(I+J); K:=L+M/N; O:= A+G-K; Less efficient than O:= ((B+C/D)-E^F)+ (H*(I+J))-(L+M/N); Or does it end up being the same when it's done? I have a tendency to avoid using variables even though the code would be infinitely more understandable if it was broken into sections and more variables were used, but I'm wondering if this is really any more efficient or if it just seems like it should be... after all storing things in a variable and reading them out should take some time... but then again the processor only works on one operation at a time anyway, so the final assembly code would probably have to store things somewhere and get them back... so.. is it really faster? So if I never need the value a second time, is it really better to avoid variables? I could break it up an use comments: O:= ((B+C/D)-E^F)+ //A (H*(I+J))- //G (L+M/N); //K This got me thinking, it would be cool if there was some king of utility that counted clock cycles for you while your program ran..and then I could just try a sample program both ways and see what the results are... James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal> ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
Hi, I noticed this too a few years ago when I translated xxHash from C to FPC. Pushing the code to less lines improved speed so xxHash32 was as fast as in C. do { v1 = XXH32_round(v1, XXH_get32bits(input)); input += 4; v2 = XXH32_round(v2, XXH_get32bits(input)); input += 4; v3 = XXH32_round(v3, XXH_get32bits(input)); input += 4; v4 = XXH32_round(v4, XXH_get32bits(input)); input += 4; } while (input < limit); repeat v1 := cPrime32x1 * RolDWord(v1 + cPrime32x2 * PLongWord(ABuffer)^, 13); v2 := cPrime32x1 * RolDWord(v2 + cPrime32x2 * PLongWord(ABuffer+4)^, 13); v3 := cPrime32x1 * RolDWord(v3 + cPrime32x2 * PLongWord(ABuffer+8)^, 13); v4 := cPrime32x1 * RolDWord(v4 + cPrime32x2 * PLongWord(ABuffer+12)^, 13); inc(ABuffer, 16); until not (ABuffer <= pLimit); __ Od: "James Richters via fpc-pascal" Komu: "'FPC-Pascal users discussions'" Datum: 09.11.2022 13:47 Předmět: [fpc-pascal] Program efficiency I was wondering if breaking up large complicated formulas into smaller sections that use more variables is less efficient than just the large formula. In other words.. is A:=(B+C/D)-E^F; G:=H*(I+J); K:=L+M/N; O:= A+G-K; Less efficient than O:= ((B+C/D)-E^F)+ (H*(I+J))-(L+M/N); James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal> ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Program efficiency
James Richters via fpc-pascal schrieb am Mi., 9. Nov. 2022, 13:47: > In other words.. is > > A:=(B+C/D)-E^F; > G:=H*(I+J); > K:=L+M/N; > O:= A+G-K; > > Less efficient than > O:= ((B+C/D)-E^F)+ (H*(I+J))-(L+M/N); > > Or does it end up being the same when it's done? > The compiler will insert temporary variables anyway when necessary (so even your first example might infact contain more variables and your second will very likely contain temporaries). Using optimizations (especially regvars, used by default in -O2) might improve this independent of whether you use variables yourself or not. But in general: you should look at other things to optimize and only come back to this when you have nothing easier to improve. Regards, Sven > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] Program efficiency
I was wondering if breaking up large complicated formulas into smaller sections that use more variables is less efficient than just the large formula. In other words.. is A:=(B+C/D)-E^F; G:=H*(I+J); K:=L+M/N; O:= A+G-K; Less efficient than O:= ((B+C/D)-E^F)+ (H*(I+J))-(L+M/N); Or does it end up being the same when it's done? I have a tendency to avoid using variables even though the code would be infinitely more understandable if it was broken into sections and more variables were used, but I'm wondering if this is really any more efficient or if it just seems like it should be... after all storing things in a variable and reading them out should take some time... but then again the processor only works on one operation at a time anyway, so the final assembly code would probably have to store things somewhere and get them back... so.. is it really faster? So if I never need the value a second time, is it really better to avoid variables? I could break it up an use comments: O:= ((B+C/D)-E^F)+ //A (H*(I+J))- //G (L+M/N); //K This got me thinking, it would be cool if there was some king of utility that counted clock cycles for you while your program ran..and then I could just try a sample program both ways and see what the results are... James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal