Strange speed up with unused variables

Just starting out? Need help? Post your questions and find answers here.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Strange speed up with unused variables

Post by Saki »

Strange speed up with unused variables :x

Now this is not a doomsday scenario, but still very amazing.

Image
地球上の平和
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Strange speed up with unused variables

Post by infratec »

Please post a 'working' example.
Else no one can inspect what's the reason.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Strange speed up with unused variables

Post by Saki »

This is just an early working code.
It is related to the array.
If the array is replaced by a list, the effect shrinks to about 40ms.
The array has been created only temporarily, for simplicity.
But it doesn't matter here, the effect is absolutely uncool.
I think it will be fixed with the new compiler.
This code is primarily meant to test the new compiler when it is ready.
If you change the position of the array in the code, the strength of the effect also changes

Code: Select all

EnableExplicit

Procedure.s StringField_BF(string$, index, separator$)
  ; StringField - By Saki - Unicode - This code is free for using and enhancing
  Protected i, ii, comp, result$
  Protected len_string=(StringByteLength(string$))
  Protected len_separator=StringByteLength(separator$)
  Dim index(len_string)
  Protected *string=@string$
  Protected *separator=@separator$
  If comp=CompareMemory(@string$, @separator$, len_separator)
    Protected jump_in=1 : i=-2 : index+1
  EndIf
  Repeat
    comp=CompareMemory(@string$+i, @separator$, len_separator)
    If comp Or jump_in : jump_in=0 : ii+1 : index(ii)=i+2 : i+len_separator-2 : index(0)+1 : EndIf : i+2
  Until i>len_string-len_separator
  index(0)=ii : i=0 
  If index(0)
    Repeat
      If index(i)
        If index(i+1)-index(i)>0
          result$=Space((index(i+1)-index(i))/2-len_separator/2)
          CopyMemory(@string$+index(i)+len_separator-2, @result$, index(i+1)-index(i)-len_separator)
          If i=index : ProcedureReturn result$ : EndIf
        EndIf
      EndIf
      i+1
    Until i=index(0)
  EndIf
EndProcedure

Define separator$="*"
Define index, result$, string$

string$="*Hello*I*am*a*splitted*string*"

Define multiplier=15

Define i
For i=1 To multiplier
   string$+string$
Next i
 Debug Len(string$)
 
 Define time=ElapsedMilliseconds()
 
For index=1 To 6*multiplier
result$=StringField_BF(string$, index, separator$)
If result$<>"" : Debug result$ : EndIf
Next 

MessageRequester("", Str(ElapsedMilliseconds()-time))
地球上の平和
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Strange speed up with unused variables

Post by infratec »

Hi Saki,

I can not replicate your results.
It doesn't matter if I comment out the 2 commands, the time is always arround 820ms.

Win10 x64 using PB 5.73 x86
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Strange speed up with unused variables

Post by Saki »

Hi infratec, i use 573 x64 on Win10.
I wouldn't have responded to that per se if it wasn't so much and reproducible.
I wanted to wait and see what the individual users produce on the various OS,
it was yesterday already too late to even test further.

I have now tested n x86, 600 without variables, 660 with variables.
This is many faster as on x64.
This is more and more strange.

If I make the array five times larger, the values increase to 1600 to 1800, but it is then reversed.
Without the variables it is then faster.

If I make it 50x bigger, it will already be 12 seconds.
This is no longer acceptable.
The increase is then due to the allocation of this large array.
Whereby with 32GB Ram and a constant array size, no such problems should be expected here.

On AMD 3900x / Core boost off and standard preset.

It always fluctuates about 10+- ms, which is within the normal range.
Last edited by Saki on Fri May 07, 2021 8:24 am, edited 1 time in total.
地球上の平和
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Strange speed up with unused variables

Post by BarryG »

Saki, when I test your code it takes 13000 ms on my PC (a long 13 seconds) because of your custom StringField_BF(). When I use the native StringField() instead, it produces exactly the same debug outputs but only takes 12 ms (instant).
Last edited by BarryG on Fri May 07, 2021 11:24 am, edited 1 time in total.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Strange speed up with unused variables

Post by Saki »

Barry, dear, this is here not about the StringField at all.

You probably can't imagine how much I don't care how StrigField works, as long as it works.

That I see it differently with the first field to be localized is just so.
Everything can be 100% correct, but I would do it differently, because it confuses.
Everything that confuses is not perfect, it's that simple !

This code here has alpha status, it is extremely slow and has bugs, that is normal, the final one is extremely faster.
The chunk here is parsing the string.
In the other StringFild_BF code the string is not parsed before, it is much faster.

But it's just test codes, like people make codes to run snails across the desktop.
Nobody needs that, but people do it anyway.


I do such things to better understand contexts and to improve my programming skills.
That's all.


The StringField_BF is just a test code to test different approaches and their efficiency.

The misbehavior described here has nothing to do with StringField at all, it's a weird array thing.

I have to work now, otherwise I would have tried to see if the effect occurs with AllocateMemory.

That's why I didn't want to post any code here at first, because I thought that this would start again
with this discussion about things that don't interest me at all.

One of the big problems with PB is the efficient handling of strings.
This requires a bit of practice.
Corona gives me the time at the moment to deal with such nonsense as the StringField_BF.
It distracts a bit from the problems that have become existential due to Corona and the constant LockDowns, that's all.
地球上の平和
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Strange speed up with unused variables

Post by Cyllceaux »

This is really strange:

with variables: 1322
without variables: 1476

If I comment "EnableExplicit"
with variables: 1399
without variables: 1388

than I changed this

Code: Select all

Protected Dim index.l(len_string)
with variables: 1061
without variables: 1041

This is interesting.
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: Strange speed up with unused variables

Post by Lord »

It's not reproducable here.
I get times between 1740 and 1760 ms.
Sometimes with commented out variables its faster but also
sometimes with the both variables active protected.
Image
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Strange speed up with unused variables

Post by #NULL »

Can't reproduce that either. PB 5.73 LTS Linux x64. Also tried compiler options like purifier, on error, threadsafe. Debugger disabled. Both are around or about 2000ms. Sometimes it's like 20ms faster without the 2 protected lines, which makes some sense due to smaller procedure stack size(?). But I don't get a significant difference, especially not faster when lines included.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Strange speed up with unused variables

Post by #NULL »

Here are some tests I did. Still I get no surprising results and barely any differences.

case 0: without the variables
case 1: only declaration, no assigment
case 2: declaration and dummy assigment, not accessing string$ / separator$
case 3: your lines included (declaration / assignment)
case 4: assignment of variables to other protected vars which are declared always and in every test case.
case 5: declaration and assignment compiled, but assignment not done at runtime

Code: Select all

EnableExplicit

Procedure.s StringField_BF(string$, index, separator$)
  ; StringField - By Saki - Unicode - This code is free for using and enhancing
  Protected i, ii, comp, result$
  Protected len_string=(StringByteLength(string$))
  Protected len_separator=StringByteLength(separator$)
  Dim index(len_string)
  
  ; -------------------------
  
  Protected *string__2
  Protected *separator__2
  
  CompilerSelect 0
    CompilerCase 0
      ;Protected *string
      ;Protected *separator
    CompilerCase 1
      Protected *string
      Protected *separator
    CompilerCase 2
      Protected *string = 0
      Protected *separator = 0
    CompilerCase 3
      Protected *string = @string$
      Protected *separator = @separator$
    CompilerCase 4
      *string__2 = @string$
      *separator__2 = @separator$
    CompilerCase 5
      Protected *string
      Protected *separator
      If index = -99 ; false runtime check, not compiler-elidable (my guess)
        *string = @string$
        *separator = @separator$
      EndIf
  CompilerEndSelect
  
  ; -------------------------
  
  If comp=CompareMemory(@string$, @separator$, len_separator)
    Protected jump_in=1 : i=-2 : index+1
  EndIf
  Repeat
    comp=CompareMemory(@string$+i, @separator$, len_separator)
    If comp Or jump_in : jump_in=0 : ii+1 : index(ii)=i+2 : i+len_separator-2 : index(0)+1 : EndIf : i+2
  Until i>len_string-len_separator
  index(0)=ii : i=0 
  If index(0)
    Repeat
      If index(i)
        If index(i+1)-index(i)>0
          result$=Space((index(i+1)-index(i))/2-len_separator/2)
          CopyMemory(@string$+index(i)+len_separator-2, @result$, index(i+1)-index(i)-len_separator)
          If i=index : ProcedureReturn result$ : EndIf
        EndIf
      EndIf
      i+1
    Until i=index(0)
  EndIf
EndProcedure

Define separator$="*"
Define index, result$, string$

string$="*Hello*I*am*a*splitted*string*"

Define multiplier=15

Define i
For i=1 To multiplier
   string$+string$
Next i
 Debug Len(string$)
 
 Define time=ElapsedMilliseconds()
 
For index=1 To 6*multiplier
result$=StringField_BF(string$, index, separator$)
If result$<>"" : Debug result$ : EndIf
Next 

MessageRequester("", Str(ElapsedMilliseconds()-time))
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Strange speed up with unused variables

Post by NicTheQuick »

I've got these results after running five times in a row:

Code: Select all

; with the two lines
452
436
420
419
422
avg=429,8 ms

; without the two lines
433
437
438
433
430
avg=434,2 ms
I don't think that's a major difference. However the reason could be how the variables on the stack are aligned. Depending on how the alignment in the memory is (dividable by 4, 8 or 16) and how long your CPUs cache lines are there can be speed differences if variables are defined or not. Of course a compiler should optimize away unused variables but I am not sure if Purebasic does that.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Strange speed up with unused variables

Post by cas »

My results with PBx64 with 10 runs:
with vars: 1222-1263
vars commented out: 1245-1290
Barely any difference (~2%) compared to yours (~25% difference).

Can you also test this code: viewtopic.php?p=567877#p567877 ?
I have 30% speed difference on my system with that code snippet between threadsafe and non threadsafe compiled code. Crazy thing is that it is threadsafe code that is faster. But others have reported that there is absolutely no difference on their systems (just like in this thread).
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Strange speed up with unused variables

Post by Saki »

Hi cas

Without Thread save 5014 / 42.855
With Thread save 5029 / 62.086
地球上の平和
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Strange speed up with unused variables

Post by Bitblazer »

The answer is very likely hidden here or here
Last edited by Bitblazer on Fri May 07, 2021 4:09 pm, edited 3 times in total.
webpage - discord chat links -> purebasic GPT4All
Post Reply