Discuss Scratch

PullJosh
Scratcher
1000+ posts

Fastest way to check divisibility?

Which is faster?
round15/3=15/3
or
15mod3=0

Or is there an even better way I haven't thought of?
Znapi
Scratcher
500+ posts

Fastest way to check divisibility?

Mod would probably be faster. Profile it yourself?
bobbybee
Scratcher
1000+ posts

Fastest way to check divisibility?

Znapi wrote:

Mod would probably be faster. Profile it yourself?
PullJosh
Scratcher
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)
BookOwl
Scratcher
1000+ posts

Fastest way to check divisibility?

In my testing, it took 1.18 secs to compute
15mod3=0
100,000 times, and 1.45 secs to compute
round15/3=15/3
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
Scratcher
1000+ posts

Fastest way to check divisibility?

Also, microoptimizations are badddd.
Thepuzzlegame
Scratcher
1000+ posts

Fastest way to check divisibility?

bobbybee wrote:

Also, microoptimizations are badddd.
More like unnecessary.
PullJosh
Scratcher
1000+ posts

Fastest way to check divisibility?

Thepuzzlegame wrote:

bobbybee wrote:

Also, microoptimizations are badddd.
More like unnecessary.
I wouldn't have done it if I thought it would mean so little.
bobbybee
Scratcher
1000+ posts

Fastest way to check divisibility?

PullJosh wrote:

Thepuzzlegame wrote:

bobbybee wrote:

Also, microoptimizations are badddd.
More like unnecessary.
I wouldn't have done it if I thought it would mean so little.
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.

As long as this code isn't being called in a loop or something, you should be good.
PullJosh
Scratcher
1000+ posts

Fastest way to check divisibility?

bobbybee wrote:

PullJosh wrote:

Thepuzzlegame wrote:

bobbybee wrote:

Also, microoptimizations are badddd.
More like unnecessary.
I wouldn't have done it if I thought it would mean so little.
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.

As long as this code isn't being called in a loop or something, you should be good.
Good to know.
MartinBraendli
Scratcher
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
Scratcher
1000+ posts

Fastest way to check divisibility?

MartinBraendli wrote:

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().
For that matter, test it on every programming language you can think of!
MartinBraendli
Scratcher
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
Scratcher
1000+ posts

Fastest way to check divisibility?

MartinBraendli wrote:

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).
It would be interesting to see exactly how much difference it makes in other languages.
__init__
Scratcher
1000+ posts

Fastest way to check divisibility?

fastest =
setito0repeatuntili=15additowhole numbers until 15 changeiby1setjto0setis-divisibletofalserepeatuntilj=15if15/3=jthensetis-divisibletotruechangejby1
totes

Last edited by __init__ (Aug. 20, 2015 21:50:02)

ev3coolexit987654
Scratcher
1000+ posts

Fastest way to check divisibility?

__init__ wrote:

fastest =
setito0repeatuntili=15additowhole numbers until 15 changeiby1setjto0setis-divisibletofalserepeatuntilj=15if15/3=jthensetis-divisibletotruechangejby1
totes
fixed
Edit: Is i necessary?

Last edited by ev3coolexit987654 (Aug. 20, 2015 21:42:29)

bobbybee
Scratcher
1000+ posts

Fastest way to check divisibility?

ev3coolexit987654 wrote:

__init__ wrote:

fastest =
setito0repeatuntili=15additowhole numbers until 15 changeiby1setjto0setis-divisibletofalserepeatuntilj=15if15/3=jthensetis-divisibletotruechangejby1
totes
fixed
Edit: Is i necessary?
…. Pretty much none of that script is necessary
MartinBraendli
Scratcher
86 posts

Fastest way to check divisibility?

__init__ wrote:

fastest =
setito0repeatuntili=15additowhole numbers until 15 changeiby1setjto0setis-divisibletofalserepeatuntilj=15if15/3=jthensetis-divisibletotruechangejby1
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
Scratcher
1000+ posts

Fastest way to check divisibility?

MartinBraendli wrote:

__init__ wrote:

fastest =
setito0repeatuntili=15additowhole numbers until 15 changeiby1setjto0setis-divisibletofalserepeatuntilj=15if15/3=jthensetis-divisibletotruechangejby1
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).
Darn… I was hoping for the karma-yoga.

Powered by DjangoBB