Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Fastest way to check divisibility?
- PullJosh
-
1000+ posts
Fastest way to check divisibility?
Which is faster?
Or is there an even better way I haven't thought of?
or
Or is there an even better way I haven't thought of?
- Znapi
-
500+ posts
Fastest way to check divisibility?
Mod would probably be faster. Profile it yourself?
- bobbybee
-
1000+ posts
Fastest way to check divisibility?
Mod would probably be faster. Profile it yourself?
- PullJosh
-
1000+ posts
Fastest way to check divisibility?
Assuming this is a fair test, mod is faster (matching your prediction).
With the list n showing, mod time was 41.611 and round time was 44.897. With the list hidden, it went much faster (as expected). However, the ratios between the times were quite similar to the original test.
(Not sure why I didn't think to test it myself… Thanks for reminding me that that's an option. :3)
With the list n showing, mod time was 41.611 and round time was 44.897. With the list hidden, it went much faster (as expected). However, the ratios between the times were quite similar to the original test.
(Not sure why I didn't think to test it myself… Thanks for reminding me that that's an option. :3)
- BookOwl
-
1000+ posts
Fastest way to check divisibility?
In my testing, it took 1.18 secs to compute
I think that the mod test is clearer though, as long as you know what ‘mod’ is.
100,000 times, and 1.45 secs to compute
100,000 times. That means it took around 0.0000118 secs to do each mod test, and 0.0000145 to do the other test.
I think that the mod test is clearer though, as long as you know what ‘mod’ is.
- bobbybee
-
1000+ posts
Fastest way to check divisibility?
Also, microoptimizations are badddd.
- Thepuzzlegame
-
1000+ posts
Fastest way to check divisibility?
More like unnecessary. Also, microoptimizations are badddd.
- PullJosh
-
1000+ posts
Fastest way to check divisibility?
I wouldn't have done it if I thought it would mean so little.More like unnecessary. Also, microoptimizations are badddd.

- bobbybee
-
1000+ posts
Fastest way to check divisibility?
As a rule of thumb, reporters are fast – this is true in a lot of high level languages, with some gotchas once in a while.I wouldn't have done it if I thought it would mean so little.More like unnecessary. Also, microoptimizations are badddd.
As long as this code isn't being called in a loop or something, you should be good.
- PullJosh
-
1000+ posts
Fastest way to check divisibility?
Good to know.As a rule of thumb, reporters are fast – this is true in a lot of high level languages, with some gotchas once in a while.I wouldn't have done it if I thought it would mean so little.More like unnecessary. Also, microoptimizations are badddd.
As long as this code isn't being called in a loop or something, you should be good.

- MartinBraendli
-
86 posts
Fastest way to check divisibility?
The conclusion “it probably doesnt matter which one you pick” is right, however your speed tests underestimate the margin of difference (div-round takes more than 50% longer than modulo). Do your tests inside a function which is run without screen refresh and leave away constant time factors like random(), setVar(), and addElementToList().
- WooHooBoy
-
1000+ posts
Fastest way to check divisibility?
For that matter, test it on every programming language you can think of! The conclusion “it probably doesnt matter which one you pick” is right, however your speed tests underestimate the margin of difference (div-round takes more than 50% longer than modulo). Do your tests inside a function which is run without screen refresh and leave away constant time factors like random(), setVar(), and addElementToList().
- MartinBraendli
-
86 posts
Fastest way to check divisibility?
Why? Modulo (integer operation) will allways be faster than Division (floating point operation), especially since you need 3 flops to check for divisibility. So I think the Modulo thing will be faster in any programming language (except If you run it on a super exotic processeor, which for reasons unclear to me, processes floating point operations faster than integer operations).
- WooHooBoy
-
1000+ posts
Fastest way to check divisibility?
It would be interesting to see exactly how much difference it makes in other languages. Why? Modulo (integer operation) will allways be faster than Division (floating point operation), especially since you need 3 flops to check for divisibility. So I think the Modulo thing will be faster in any programming language (except If you run it on a super exotic processeor, which for reasons unclear to me, processes floating point operations faster than integer operations).
- __init__
-
1000+ posts
Fastest way to check divisibility?
fastest =
totes
Last edited by __init__ (Aug. 20, 2015 21:50:02)
- ev3coolexit987654
-
1000+ posts
Fastest way to check divisibility?
fixed fastest =totes
Edit: Is i necessary?
Last edited by ev3coolexit987654 (Aug. 20, 2015 21:42:29)
- bobbybee
-
1000+ posts
Fastest way to check divisibility?
…. Pretty much none of that script is necessaryfixed fastest =totes
Edit: Is i necessary?

- MartinBraendli
-
86 posts
Fastest way to check divisibility?
fastest =totes
I think think this is a joke, like “esotheric algorithms”. If you have never heard of esotheric algorithms, go check out bogobogosort. The site also has other intersting esotheric stuff (programming context, not the karma-yoga stuff).
- PullJosh
-
1000+ posts
Fastest way to check divisibility?
Darn… I was hoping for the karma-yoga.fastest =totes
I think think this is a joke, like “esotheric algorithms”. If you have never heard of esotheric algorithms, go check out bogobogosort. The site also has other intersting esotheric stuff (programming context, not the karma-yoga stuff).
- Discussion Forums
- » Advanced Topics
-
» Fastest way to check divisibility?