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

Using $x*$x is 2x faster than $x**2 #92

Merged
merged 2 commits into from
Jul 1, 2024
Merged

Conversation

AkmalFairuz
Copy link
Contributor

Using $x**2

<?php
class Vector3 {
    public $x;
    public $y;
    public $z;

    public function __construct(float|int $x, float|int $y, float|int $z) {
        $this->x = $x;
        $this->y = $y;
        $this->z = $z;
    }

    public function distanceSquared(Vector3 $pos): float {
        return (($this->x - $pos->x) ** 2) + (($this->y - $pos->y) ** 2) + (($this->z - $pos->z) ** 2);
    }
}

$vector1 = new Vector3(1.0, 2.0, 3.0);
$vector2 = new Vector3(4.0, 5.0, 6.0);
$start = microtime(true);
for($i = 0; $i < 5000000; $i++){
  $res = $vector1->distanceSquared($vector2);
}
var_dump($res);
var_dump(microtime(true) - $start);
// output
// float(27)
// float(0.7717170715332031)

Using $x*$x

<?php
class Vector3 {
    public $x;
    public $y;
    public $z;

    public function __construct(float|int $x, float|int $y, float|int $z) {
        $this->x = $x;
        $this->y = $y;
        $this->z = $z;
    }

    public function distanceSquared(Vector3 $pos): float {
        $dx = $this->x - $pos->x;
        $dy = $this->y - $pos->y;
        $dz = $this->z - $pos->z;
        return ($dx * $dx) + ($dy * $dy) + ($dz * $dz);
    }
}

$vector1 = new Vector3(1.0, 2.0, 3.0);
$vector2 = new Vector3(4.0, 5.0, 6.0);
$start = microtime(true);
for($i = 0; $i < 5000000; $i++){
  $res = $vector1->distanceSquared($vector2);
}
var_dump($res);
var_dump(microtime(true) - $start);
// output
// float(27)
// float(0.5707399845123291)

@AkmalFairuz AkmalFairuz deleted the branch pmmp:stable June 6, 2024 09:18
@AkmalFairuz AkmalFairuz closed this Jun 6, 2024
@AkmalFairuz AkmalFairuz deleted the stable branch June 6, 2024 09:18
@AkmalFairuz AkmalFairuz restored the stable branch June 6, 2024 09:19
@AkmalFairuz AkmalFairuz reopened this Jun 6, 2024
@dadodasyra
Copy link

This is ridiculous but true (and more than 30%).

$x**2
0.6087791919708252
and $x*$x
0.2943990230560303
With a Windows 11 php 8.2.11, i7-12700kf
So more than 50%

@AkmalFairuz
Copy link
Contributor Author

AkmalFairuz commented Jun 30, 2024

This is ridiculous but true (and more than 30%).

$x**2 0.6087791919708252 and $x*$x 0.2943990230560303 With a Windows 11 php 8.2.11, i7-12700kf So more than 50%

That's 2x faster.

I also tested locally using an i5 10400F CPU, here are the results:

$x**2

float(27)
float(1.1193900108337402)

$x*$x

float(27)
float(0.5409140586853027)

It's really 2 times faster. $x**2 is often used in PM, especially in the Vector3->distanceSquared() method

@AkmalFairuz AkmalFairuz changed the title Using $x*$x is 30% faster than $x**2 Using $x*$x is 2x faster than $x**2 Jun 30, 2024
@SOF3
Copy link
Member

SOF3 commented Jul 1, 2024

I have no opposition to this, just noting that distanceSquared doesn't seem to be a hot path.

@SOF3 SOF3 merged commit e7a2125 into pmmp:stable Jul 1, 2024
6 checks passed
dktapps added a commit that referenced this pull request Dec 3, 2024
github web editor try not to suck challenge IMPOSSIBLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants