Skip to content

Commit ec58430

Browse files
authored
PHP/solution_1: Optimisation (PlummersSoftwareLLC#846)
1 parent 38973f4 commit ec58430

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

PrimePHP/solution_1/Dockerfile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
FROM php:8.0-cli-alpine3.13
1+
FROM php:8.1.7-cli
22

3-
WORKDIR /opt/app
3+
# Use the default production configuration
4+
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
5+
# Enable opcache & JIT
6+
RUN printf "zend_extension=opcache.so\nopcache.enable=1\nopcache.enable_cli=1\nopcache.jit_buffer_size=100M\nopcache.jit=tracing\nmemory_limit=-1\n" >> "$PHP_INI_DIR/php.ini"
47

8+
WORKDIR /opt/app
59
COPY *.php .
6-
COPY opcache.ini /usr/local/etc/php/conf.d/
710

811
ENTRYPOINT [ "php", "PrimePHP.php" ]

PrimePHP/solution_1/PrimePHP.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class PrimeSieve
1616
{
17-
private SplFixedArray $rawbits;
17+
private string $rawbits;
1818

1919
private int $sieveSize;
2020
private int $rawBitsSize;
@@ -42,11 +42,12 @@ public function runSieve()
4242
$sieveSize = $this->sieveSize;
4343
$q = sqrt($sieveSize);
4444
$rawBitsSize = $this->rawBitsSize;
45-
$rb = new SplFixedArray($rawBitsSize);
45+
$rb = str_repeat('0', $rawBitsSize);
4646

4747
while ($factor < $q) {
4848
for ($i = $factor; $i <= $sieveSize; $i += 2) {
49-
if ($rb[$i * 0.5] === null) {
49+
$rbi = (int)($i * 0.5);
50+
if ($rb[$rbi] == '0') {
5051
$factor = $i;
5152
break;
5253
}
@@ -55,7 +56,7 @@ public function runSieve()
5556
$ft2 = $factor;
5657
$start = (int)($factor * $factor * 0.5);
5758
for ($i = $start; $i < $rawBitsSize; $i += $ft2) {
58-
$rb[$i] = 1;
59+
$rb[$i] = '1';
5960
}
6061

6162
$factor += 2;
@@ -66,7 +67,7 @@ public function runSieve()
6667
public function printResults(): void
6768
{
6869
for ($i = 1; $i < $this->sieveSize; $i++) {
69-
if ($i % 2 && $this->rawbits[$i * 0.5] === null) {
70+
if ($i % 2 && $this->rawbits[(int)($i * 0.5)] == '0') {
7071
echo $i . ", ";
7172
}
7273
}
@@ -75,9 +76,9 @@ public function printResults(): void
7576
public function getRawbitCount(): int
7677
{
7778
$sum = 0;
78-
$sz = $this->rawbits->getSize();
79+
$sz = strlen($this->rawbits);
7980
for ($i = 0; $i < $sz; $i++){
80-
if ($this->rawbits[$i] === null){
81+
if ($this->rawbits[$i] == '0'){
8182
$sum++;
8283
}
8384
}
@@ -120,7 +121,7 @@ public function getRawbitCount(): int
120121

121122
// Following 2 lines added by rbergen to conform to drag race output format
122123
echo "\n\n";
123-
printf("DennisdeBest;%d;%f;1;algorithm=base,faithful=yes\n", $passes, ((float)$tD) / 1000);
124+
printf("DennisdeBest;%d;%f;1;algorithm=base,faithful=yes,bits=8\n", $passes, ((float)$tD) / 1000);
124125

125126
function getTimeDiffInMs(float $tStart): float
126127
{

PrimePHP/solution_1/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ Leaned the loop logic.
66
Uses factor * factor.
77
Enabled JIT, small improvement in docker, native it gives me about 30% boost.
88

9+
Optimisations by sqonk:
10+
Changing SPLFixedArray to a simple string yields a nearly 2x performance increase.
11+
912
![Algorithm](https://img.shields.io/badge/Algorithm-base-green)
1013
![Faithfulness](https://img.shields.io/badge/Faithful-yes-green)
1114
![Parallelism](https://img.shields.io/badge/Parallel-no-green)
12-
![Bit count](https://img.shields.io/badge/Bits-unknown-yellowgreen)
15+
![Bit count](https://img.shields.io/badge/Bits-8-yellowgreen)

PrimePHP/solution_1/opcache.ini

-5
This file was deleted.

0 commit comments

Comments
 (0)