[php-src] PR #14894: ext/random: Optimized `getBytesFromString`

From: Date: Wed, 10 Jul 2024 06:03:48 +0000
Subject: [php-src] PR #14894: ext/random: Optimized `getBytesFromString`
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-27163@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/14894 Author: SakiTakamachi All measurement results are from the latest commit. ## Benchmark codes Since the description is too long, <?php and use are omitted. ### Using PcgOneseq128XslRr64 0.php: ``` $r = new Randomizer(new PcgOneseq128XslRr64()); // 256 characters $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 10000000; $i++) { $r->getBytesFromString($str, 1); } ``` 1.php: ``` $r = new Randomizer(new PcgOneseq128XslRr64()); $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 10000000; $i++) { $r->getBytesFromString($str, 16); } ``` 2.php ``` $r = new Randomizer(new PcgOneseq128XslRr64()); $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 300000; $i++) { $r->getBytesFromString($str, 1024); } ``` ### Omit constructor arguments n0.php: ``` $r = new Randomizer(); $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 1000000; $i++) { $r->getBytesFromString($str, 1); } ``` n1.php: ``` $r = new Randomizer(); $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 500000; $i++) { $r->getBytesFromString($str, 16); } ``` n2.php: ``` $r = new Randomizer(); $str = '1GCOFExQwWVNBFxOpGXilQdjcCrS6CxJRbgRf214G6w0bnFdwlmylDIpAQlcHVH5co5heIrarVoodaGeTbMQwWaM16fwXfLr03tjYymp2hg0XI5MqTlEr1taXFyHD5fYNqX7nkFJKOYU3FRMeIUkXAMpqqkIdNXP2od5Wbkra5rvLV4WT1lGN0Yg0AeEXAPq8nRxdePU2M6vQOX7wvJKKrmMAWsopEEDAdOxtrOb8lp7oPI3RjhXMbTFXaPgjljO'; for ($i = 0; $i < 5000; $i++) { $r->getBytesFromString($str, 1024); } ``` ## Using PcgOneseq128XslRr64 ### before ``` # hyperfine "php /mount/random/fromstr/0.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/0.php Time (mean ± σ): 358.9 ms ± 15.1 ms [User: 353.9 ms, System: 4.1 ms] Range (min … max): 345.0 ms … 396.3 ms 10 runs # hyperfine "php /mount/random/fromstr/1.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/1.php Time (mean ± σ): 619.4 ms ± 6.4 ms [User: 615.3 ms, System: 3.1 ms] Range (min … max): 609.5 ms … 629.8 ms 10 runs # hyperfine "php /mount/random/fromstr/2.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/2.php Time (mean ± σ): 637.1 ms ± 7.4 ms [User: 632.7 ms, System: 3.4 ms] Range (min … max): 629.0 ms … 652.2 ms 10 runs ``` ### after ``` # hyperfine "php /mount/random/fromstr/0.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/0.php Time (mean ± σ): 343.2 ms ± 7.5 ms [User: 338.1 ms, System: 4.3 ms] Range (min … max): 334.8 ms … 359.9 ms 10 runs # hyperfine "php /mount/random/fromstr/1.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/1.php Time (mean ± σ): 482.4 ms ± 13.8 ms [User: 478.1 ms, System: 3.3 ms] Range (min … max): 471.1 ms … 520.4 ms 10 runs # hyperfine "php /mount/random/fromstr/2.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/2.php Time (mean ± σ): 366.6 ms ± 7.9 ms [User: 362.5 ms, System: 3.3 ms] Range (min … max): 360.9 ms … 388.0 ms 10 runs ``` ## Omit constructor arguments ### before ``` # hyperfine "php /mount/random/fromstr/n0.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n0.php Time (mean ± σ): 502.3 ms ± 9.4 ms [User: 197.5 ms, System: 303.9 ms] Range (min … max): 489.1 ms … 517.7 ms 10 runs # hyperfine "php /mount/random/fromstr/n1.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n1.php Time (mean ± σ): 500.7 ms ± 7.6 ms [User: 181.6 ms, System: 318.1 ms] Range (min … max): 492.4 ms … 510.8 ms 10 runs # hyperfine "php /mount/random/fromstr/n2.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n2.php Time (mean ± σ): 315.2 ms ± 9.5 ms [User: 103.6 ms, System: 210.8 ms] Range (min … max): 306.0 ms … 334.3 ms 10 runs ``` ### after ``` # hyperfine "php /mount/random/fromstr/n0.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n0.php Time (mean ± σ): 482.5 ms ± 5.6 ms [User: 179.3 ms, System: 302.5 ms] Range (min … max): 476.7 ms … 494.3 ms 10 runs # hyperfine "php /mount/random/fromstr/n1.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n1.php Time (mean ± σ): 477.1 ms ± 6.9 ms [User: 172.5 ms, System: 303.8 ms] Range (min … max): 470.4 ms … 488.5 ms 10 runs # hyperfine "php /mount/random/fromstr/n2.php" --warmup 10 Benchmark 1: php /mount/random/fromstr/n2.php Time (mean ± σ): 298.3 ms ± 4.3 ms [User: 99.3 ms, System: 198.3 ms] Range (min … max): 293.1 ms … 306.4 ms 10 runs ```

« previous php.git-pulls (#27163) next »