Author Topic: Something wired  (Read 3643 times)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Something wired
« on: 2012-Jun-15 »
Ok i just found something really wired regarding to speed of GLB:
Code: (glbasic) [Select]
LOCAL start=0
LOCAL stop=200000
LOCAL stepping=0.005

time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=1
NEXT
time0=GETTIMERALL()-time0
PRINT time0+" ms", 0,0
Displayed time after execution: about 89 ms

Code: (glbasic) [Select]
LOCAL start=0
LOCAL stop=200000
LOCAL stepping=0.005

time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=1
NEXT
time0=GETTIMERALL()-time0
LOCAL s$=value
PRINT time0+" ms", 0,0
Displayed time after execution: about 112 ms

Im checking time for exactly the same code in both cases, but its significant slower if i convert the result to a string in an other part of the program (displaying the result with PRINT has the same effect). Does anyone has an idea why this happens?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline theprotocol

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Something wired
« Reply #1 on: 2012-Jun-15 »
My guess:

You're not strictly specifying the type of "value." Assigning it to a string later tells the compiler that "value" is a string instead of an integer. This would occur before runtime (it happens at compilation). So it's as if you wrote value$ vs. value%. The compiler is just guessing ahead of time what the variable is (your later code with s$=value changes its guess).

The performance could vary in that case between - assigning 1 to a string (probably internal parsing or casting here)- and assigning 1 to an integer (native).
« Last Edit: 2012-Jun-15 by theprotocol »

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: Something wired
« Reply #2 on: 2012-Jun-16 »
No, wrong guess, if i do not specify a type its always a float. But even if i declare it as float or integer directly i get the same result (you can try it yourself if you want).
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline theprotocol

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Something wired
« Reply #3 on: 2012-Jun-16 »
No, wrong guess, if i do not specify a type its always a float. But even if i declare it as float or integer directly i get the same result (you can try it yourself if you want).

I see. Interesting...

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 351
  • to program what I have todo how should I program?
    • View Profile
Re: Something wired
« Reply #4 on: 2012-Jun-16 »
Hi kanonet,
on my computer in both cases the times are the same: 80ms
ciao
qedo

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4396
    • View Profile
    • FUED
Re: Something wired
« Reply #5 on: 2012-Jun-16 »
just out of a noob curiosity...

If you leave declarations up to the "no strict declarations" on the options of the project/IDE.
Same results?

Not that this would answer the question at all, but it may be a help on understanding?
I will see to run those here to see what I get, but such is, beyond my understandings as a programmer.  ;)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: Something wired
« Reply #6 on: 2012-Jun-16 »
@Qedo:
Interesting i tested it on two machines with two different CPUs (Intel i5-540M and AMD C-50), both had a difference in the times. Can you use a more complicated math instead of "value=1"? E.g. when i use "value=qSIN(i)" the difference is bigger, 90ms vs. 330ms. Is there a difference for you?

@erico:
If i do not declare "value" i get:
"value=1": 150ms vs. 150-160ms
"value=qSIN(i)": 340ms vs. 340-350ms
Same results if i declare value as GLOBAL.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Moebius

  • Dr. Type
  • ****
  • Posts: 315
    • View Profile
Re: Something wired
« Reply #7 on: 2012-Jun-16 »
This is confusing.

I tried this code:
Code: (glbasic) [Select]
LOCAL start=0
LOCAL stop=200000
LOCAL stepping=0.005

LOCAL time0#, value#, s$

FOR trials = 0 TO 2

value = 0
time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=value+1
NEXT
time0=GETTIMERALL()-time0
STDOUT time0+" ms\n"


NEXT

STDOUT "\n\n"


FOR trials = 0 TO 2

value = 0
time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=value+1
NEXT
time0=GETTIMERALL()-time0
s$ = value
STDOUT time0+" ms\n"

NEXT

KEYWAIT
END

This gives me around 100ms for the pure loop, and 140ms for the loop which has "s$ = value" afterwards...


However, running this code:
Code: (glbasic) [Select]
LOCAL start=0
LOCAL stop=200000
LOCAL stepping=0.005

LOCAL time0#, value#, s$


value = 0
time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=value+1
NEXT
time0=GETTIMERALL()-time0
STDOUT time0+" ms\n"


STDOUT "\n\n"

value = 0
time0=GETTIMERALL()
FOR i=start TO stop STEP stepping
value=value+1
NEXT
time0=GETTIMERALL()-time0
s$ = value
STDOUT time0+" ms\n"


KEYWAIT
END
(The same code just w/o the for loops), I get 115ms and 140ms....

Using kakonet's original code (i.e. using 'value=1' instead of 'value=value+1'), with both snippets left in as above, I get the same times (115ms, 140ms).
Running either snippet on their own I get these times...

Using 'value = SIN(1)' instead, I get something like 4495ms for the one without the string, and 4505ms for the one with the string assignment afterwards.

I have tried things like adding a DELAY at the start and rearranging things, but get the same results.
Go figure  :S
This seems like it has to do with optimisation, although I don't understand why things would turn out differently within for loops, or with a conversion to a string afterwards....
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 351
  • to program what I have todo how should I program?
    • View Profile
Re: Something wired
« Reply #8 on: 2012-Jun-16 »
Hi kanonet,
on my computer CPUs Intel i7-2630QM GPU 540M in both cases with qSIN(i) the times are the same: 440ms +/-10.
There is no true difference.
ciao
qedo