Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Integer divison (MMB4W)
Author | Message | ||||
PEnthymeme Regular Member Joined: 27/12/2023 Location: United KingdomPosts: 42 |
Hi I am looking at integer division. timer = 0 'use integer division for x = 0 to 1000000 div= 35\11 next x print timer,div timer = 0 'use "normal" division and call integer function for x = 0 to 1000000 div= int(35/11) next x print timer,div Output: Integer division: 332ms int() function: 453ms The former (\ integer division) is roughly 20% quicker. Now "intellectually" not surprised as this is how I would do it in Python as its the way I was taught - but never really thought why this is the case. Naively I just assumed that internally division was taking place, and then internally calling the int() function. Not the case. Even if I force "div" to be an integer with dim div as integer, the difference persists. (again, not surprised) "Internally" what is "\" doing that renders it so much quicker than int() and / ? Px |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4232 |
Hi Px, This is completely as expected. The integer division is a single instruction Taking the integer value (instruction 1) of a division (instruction 2) are 2 instructions. Appart from the math that has to be performed under water, the parser has to lookup the instructions before it can execute them. Would this be a compiler language, then the compiled could could potentially be equally fast. Volhout PicomiteVGA PETSCII ROBOTS |
||||
PEnthymeme Regular Member Joined: 27/12/2023 Location: United KingdomPosts: 42 |
Cheers - yes, I see how the second requires more calls / instructions - but in my naivety sort of assumed that the first would just be a wrapper to the second. Not that the first would implement things differently. But I get it. As said, my naivety. Appreciate the feedback. And for clarity - just asking, not moaning Px |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
... also / is a floating divide, so your integers will be cast as floats for the / to take place you can see this from the below > dim integer p,n > p=35 > n=11 > ?p/n 3.18182 > you get a float type answer, not the integer (3) you were expecting Edited 2024-03-09 00:10 by CaptainBoing |
||||
PEnthymeme Regular Member Joined: 27/12/2023 Location: United KingdomPosts: 42 |
Perfect -- didn't appreciate that nuance. Px |
||||
Print this page |