Recent

Author Topic: Unexpected behavior/results  (Read 2849 times)

Selfmade.exe

  • New Member
  • *
  • Posts: 36
Unexpected behavior/results
« on: April 13, 2018, 05:33:00 pm »
Hello everyone,

straight to the issue. Looping/repeating a function to measure time needed to complete the looping (hence how fast the function is) I get different results.

by changing this:
Code: Pascal  [Select][+][-]
  1. astring:='whatever';
  2. y:=length(astring);
  3. y_minus_one:=y-1;

to this:
Code: Pascal  [Select][+][-]
  1. astring:='whatever';
  2. y:=length(astring);
  3. y_minus_one_temporary:=y-1;
  4. y_minus_one:=y_minus_one_temporary;

the program runs slower. Note that the above code is outside of the benchmarked looping function, but the function uses the y_minus_one. Any explanation?
« Last Edit: April 13, 2018, 06:06:45 pm by Selfmade.exe »

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Unexpected behavior/results
« Reply #1 on: April 13, 2018, 06:05:27 pm »
The case may be that FPC may not be able to do certain optimisations when you are using the variable in- and outside the loop. It may also be a case of internal optimisation within modern processor architectures.
I remember a similar issue, a mandelbrot benchmark running factor 2 faster simply when I added a completely unused variable to the programs main body outside the loop. Thought this was bug but learned that the many internal optimisation schemes of modern processors may interfere with each other and cause unpredictable results sometimes.

Selfmade.exe

  • New Member
  • *
  • Posts: 36
Re: Unexpected behavior/results
« Reply #2 on: April 13, 2018, 06:13:45 pm »
This is the second time I stumble on this. I have noticed it in another program of mine, but using pointer to char instead of integers. But the results were oposite (ie it was faster using intermediate variable).

Quote
I remember a similar issue, a mandelbrot benchmark running factor 2 faster simply when I added a completely unused variable to the programs main body outside the loop. Thought this was bug but learned that the many internal optimisation schemes of modern processors may interfere with each other and cause unpredictable results sometimes.
:o

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Unexpected behavior/results
« Reply #3 on: April 13, 2018, 06:45:38 pm »
The times are gone where execution speed could simply be estimated by CPU cycles and assembler instructions. There is not necessarily a linear relationship anymore. Have a look at Agner Fog's optimisation articles to get an impression of what is going on within nowaday's CPUs.

Selfmade.exe

  • New Member
  • *
  • Posts: 36
Re: Unexpected behavior/results
« Reply #4 on: April 17, 2018, 10:47:14 pm »
So whats the rule of thumb when it comes to write a program, besides this, http://www.math.uni-leipzig.de/pool/tuts/FreePascal/prog/node12.html? Now I will have to worry about inserting intermediate/empty variables too?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Unexpected behavior/results
« Reply #5 on: April 19, 2018, 02:27:21 am »
Did you check the generated code? Try -al
There could be a bug. So give us something we can try, like a small app. I don't see details. What OS, 32bit or 64bit? FPC version? etc.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Unexpected behavior/results
« Reply #6 on: April 19, 2018, 07:14:59 am »
You are also using a very old version of the programmer's guide.
Use the latest one - and official one - instead: https://www.freepascal.org/docs-html/current/prog/progch11.html#x236-25000011
But without a full example we can't test this and it may even differ between platforms. Also note: which string type? For AnsiString and ShortString it does not matter too much but for Unicode string types the speed difference can be dramatic.
« Last Edit: April 19, 2018, 07:17:52 am by Thaddy »
Specialize a type, not a var.

RayoGlauco

  • Full Member
  • ***
  • Posts: 176
  • Beers: 1567
Re: Unexpected behavior/results
« Reply #7 on: April 19, 2018, 10:36:33 am »
So whats the rule of thumb when it comes to write a program, besides this, http://www.math.uni-leipzig.de/pool/tuts/FreePascal/prog/node12.html? Now I will have to worry about inserting intermediate/empty variables too?

I think we have to optimize our code in a logical way. Speculating about what the processor will do can be counterproductive, because the code can be executed in different processors, which will optimize execution in different ways.
To err is human, but to really mess things up, you need a computer.

 

TinyPortal © 2005-2018