Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Which is faster?

Author
Message
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 6th Jan 2021 14:45 Edited at: 6th Jan 2021 14:46


or

The author of this post has marked a post as an answer.

Go to answer

PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th Jan 2021 16:02
As you're declaring the variable only once, it won't make any difference.

Theoretically, your second varible assignment is slightly faster than your first one.


I did 10 million assignments using myType.aninteger = 1 vs 10 million assignments using aninteger = 1, and the difference was about 50-100 milliseconds.

That's a difference of 0.000005 to 0.00001 milliseconds per assignment...


PSY LABS Games
Coders don't die, they just gosub without return
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 6th Jan 2021 19:05 Edited at: 6th Jan 2021 19:06
This post has been marked by the post author as the answer.


This should show you the benchmark figures... it's slower, but change the MaxCount Value; you'll notice the performance difference reduces with more iterations.
Obviously though the key issue is that the more iterations the slower the longer overall it takes.

I also included a quick'n'dirty benchmark for 2 different approaches to iterations.
For...Next is SLOWER than you might think; but I can't tell you why.

Arrays also come with a similar performance penalty to Typed Data, so keep that in mind; but I'm sure you can add a benchmark run for that.
Now as a note... if you want more accurate data, I'd recommend Multiple Runs (3-5) that are then averaged; as the more times then with averaging approaches you can remove unusually high/low scores to give a more aggregated (expectable) result.

[edit]
I should note that with very minor changes this code will also run in Dark BASIC Professional., should you be curious to see the performance difference.
Trust me, it's a very interesting result that you might not expect.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 6th Jan 2021 19:25
Declaration of type variables is slightly slower than basic globals but access speeds are pretty mush the same, for me readability trumps performance, types tidy up the code, organize vars and such.

I like to modularize everything when I write code and types help with this, if I take a tiny performance hit but have modular code I can drop into other projects then its well worth a few hundreds of a millisecond overhead lol


george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 6th Jan 2021 22:45 Edited at: 6th Jan 2021 22:52
Thank you very much

Quote: "As you're declaring the variable only once, it won't make any difference."

I declare it only once but I use the typed variable as an argument in a function call within the main application loop but I make assignments within the main application loop

@Raven: thank you very much for the code snippet. I should have thought of it before posting it

Quote: "I should note that with very minor changes this code will also run in Dark BASIC Professional"

Errr... I was a Blitz3D guy and it was super fast. I know nothing about DBPro , but I can imagine that DBPro is much faster than AppGameKit since it is a compiler, right?

Quote: "types tidy up the code, organize vars and such."

This is my dilemma... Easier code reading (and maintenance) or faster execution...?
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 7th Jan 2021 09:51
george++ wrote: "This is my dilemma... Easier code reading (and maintenance) or faster execution...?"


Well., it all depends on how much you need to read-write values.
As you'll see from the basic benchmark code I wrote., realistically the main performance overhead ISN'T from Reading / Writing the Variables themselves.

Sure, Structured Types are considerably slower ... but the biggest performance overhead stems from the Loops themselves.

I've said this in the past, and I'll repeat this again; but the biggest issue with AppGameKit Script is performance.
Performance is abysmal, even for a Scripting Language.

I'd recommend not worrying to much about using Structured Types., instead just focus on smaller projects that don't need to rely on a lot of logic or in-flight data.

Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 8th Jan 2021 02:05

All instructions have different performance, so If you need to time something it's best to unroll the operation rather than jam in within a tight loop. So your timing more of the operation and less loop overhead.

Time.Start = Timer()
For Count = 0 To CountMax
myTypeValue.AnInteger = 1
myTypeValue.AnInteger = 2
myTypeValue.AnInteger = 3
myTypeValue.AnInteger = 4
myTypeValue.AnInteger = 5
myTypeValue.AnInteger = 6
myTypeValue.AnInteger = 8
myTypeValue.AnInteger = 9
myTypeValue.AnInteger = 10
Next
Time.Stop = Timer()
Time.Duration = (Time.Stop - Time.Start) * 1000.0
Global Typed As Float : Typed = Time.Duration

Time.Start = Timer()
For Count = 0 To CountMax
myValue = 1
myValue = 2
myValue = 3
myValue = 4
myValue = 5
myValue = 6
myValue = 7
myValue = 8
myValue = 9
myValue = 10
Next
Time.Stop = Timer()


also, since reading is more prevalent than writing it's handy to time the read performance also.


Time.Start = Timer()
For Count = 0 To CountMax
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger

temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger
temp =myTypeValue.AnInteger

Next
Time.Stop = Timer()
Time.Duration = (Time.Stop - Time.Start) * 1000.0
Global Typed As Float : Typed = Time.Duration


Most gains from runtimes/VM are found in caching results that tend to be consistently computed on demand..

etc etc

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Arch-Ok
AGK Developer
4
Years of Service
User Offline
Joined: 11th Jul 2019
Location: Bursa/TÜRKIYE
Posted: 12th Jan 2021 19:51 Edited at: 12th Jan 2021 20:50
second one is faster

mytype.mydata = 1




mydata = 1
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 12th Jan 2021 21:24
@Arch-Ok: is this a disassembly?. Where did you find this?
Arch-Ok
AGK Developer
4
Years of Service
User Offline
Joined: 11th Jul 2019
Location: Bursa/TÜRKIYE
Posted: 12th Jan 2021 21:38
@george++ no it is not. very simple way of showing the difference. Also, the difference gets bigger, if you have array of types.
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 13th Jan 2021 12:55
When I was young, I was programing the 8088 on an Amstrad 1512, for sprite handling.
Thanks to your post I remembered the old good days...

Login to post a reply

Server time is: 2024-04-19 20:29:53
Your offset time is: 2024-04-19 20:29:53