Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimises ISEVEN() , ISODD() macro #10504

Merged
merged 4 commits into from
Jan 29, 2024
Merged

Conversation

EvilDragonfiend
Copy link
Member

@EvilDragonfiend EvilDragonfiend commented Jan 18, 2024

About The Pull Request

Optimises ISEVEN() , ISODD() macro

calculation is expensive than comparison bitwise operator. When we know first bitflag makes a value even or odd, we don't have to calculate to get 0 by divide 2.

Division is extremely cheap. What isn't extremely cheap is finding the remainder of division. Secondly, you aren't comparing, you're doing a bitwise math operation, comaprison is ==. Bitwise math is actually really slow in byond due to how numbers work in the engine, but it is funny it's slower than modulo.

Kapu's comment

Why It's Good For The Game

optimisation

Testing Photographs and Procedure

simulation code

#define ISEVEN(x) (!((x) & 1))
#define ISODD(x) ((x) & 1)
#define ISEVENMOD(x) (x % 2 == 0)
#define ISODDMOD(x) (x % 2 != 0)

/proc/main()
    set background = 1
    var/const/repeat = 15000000
    var/t1
    var/t2
    for(var/i in 1 to 100)
        TEST1(0)
        TEST2(0)

    var/time1 = world.timeofday 
    for(var/i in 1 to repeat)
        TEST1(i)
    t1 = world.timeofday-time1
    world.log << "bitflag &: [t1] taken"

    var/time2 = world.timeofday
    for(var/i in 1 to repeat)
        TEST2(i)
    t2 = world.timeofday-time2
    world.log << "mod %: [t2] taken"

/proc/TEST1(val)
    return ISODD(val)

/proc/TEST2(val)
    return ISODDMOD(val)

image
image

Note

image

It has the same result for decimal numbers

Changelog

🆑
code: improved ISEVEN() and ISODD() macro
/:cl:

code/__DEFINES/maths.dm Outdated Show resolved Hide resolved
Co-authored-by: itsmeow <itsmeow@itsmeow.dev>
@Penwin0 Penwin0 added this pull request to the merge queue Jan 29, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 29, 2024
@itsmeow itsmeow added this pull request to the merge queue Jan 29, 2024
Merged via the queue into BeeStation:master with commit 65bea41 Jan 29, 2024
8 checks passed
DrDuckedGoose pushed a commit to DrDuckedGoose/BeeStation-Hornet that referenced this pull request May 11, 2024
* optim even odd

* very mild optimisation

* == 0 is unnecessary

* Update code/__DEFINES/maths.dm

Co-authored-by: itsmeow <itsmeow@itsmeow.dev>

---------

Co-authored-by: itsmeow <itsmeow@itsmeow.dev>
DrDuckedGoose pushed a commit to DrDuckedGoose/BeeStation-Hornet that referenced this pull request May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants