On php ffi and performance examples

This commit is contained in:
Balhau 2025-01-08 16:02:19 +00:00
parent b0380e797d
commit 88d595a7bf
10 changed files with 135 additions and 0 deletions

5
php/Dockerfile Normal file
View file

@ -0,0 +1,5 @@
FROM php:8.2-alpine
RUN apk add --no-cache --virtual .persistent-deps libffi-dev \
&& docker-php-ext-configure ffi --with-ffi \
&& docker-php-ext-install ffi

28
php/Makefile Normal file
View file

@ -0,0 +1,28 @@
DOCKER_RUN=docker run -it --rm -v "$PWD:/usr/src/myapp" -w /usr/src/myapp php/ffi
CC=gcc
.PHONY: all
all: docker-image build-native-lib build-main-c
.PHONY: build-native-lib
build-native-lib:
${CC} -shared -o heavy_stuff.so -fPIC heavy_stuff.c
build-main-c:
${CC} main.c ./heavy_stuff.so -o main
.PHONY: docker-image
docker-image:
docker build -t php/ffi .
.PHONY: run-heavy-php
run-heavy-php:
${DOCKER_RUN} php heavy_stuff.php
.PHONY: run-heavy-php-c
run-heavy-php-c:
${DOCKER_RUN} php heavy_stuff.
.PHONY: clean
clean:
rm main heavy_stuff.so

24
php/heavy_stuff.c Normal file
View file

@ -0,0 +1,24 @@
unsigned long some_heavy_stuff(int lima, int limb) {
unsigned long result = 0;
int i, j = 0;
int sum = 0;
for (i = 0; i < lima; i++) {
sum = 0;
for (j = 0; j < limb; j++) {
sum += i + j;
}
result += sum;
}
return result;
}
unsigned long lesser_heavy_stuff(int lima, int limb) {
unsigned long result = 0;
int i, j = 0;
for (i = 0; i < lima; i++) {
// Optimize sum of first n numbers
// Factorize outer index
result += (i * limb) + ((limb - 1) * limb) / 2;
}
return result;
}

24
php/heavy_stuff.php Normal file
View file

@ -0,0 +1,24 @@
<?php
echo "Hello, heavy stuff here:\n";
function someReallyHeavyStuff($lima, $limb){
$result=0;
for($i=0;$i<$lima;$i++){
$sum=0;
for($j=0;$j<$limb;$j++){
$sum+=$i+$j;
}
$result+=$sum;
}
$result = 0;
for($i=0;$i<$lima;$i++){
$result+=$sum;
}
return $result;
}
$result = someReallyHeavyStuff(1000,1000);
echo "The heavy result: $result\n"
?>

BIN
php/heavy_stuff.so Executable file

Binary file not shown.

13
php/heavy_stuff_c.php Normal file
View file

@ -0,0 +1,13 @@
<?php
$ffi = FFI::cdef("
unsigned long some_heavy_stuff(int lima, int limb);",
"./heavy_stuff.so"
);
echo "Hello, also heavy stuff here:\n";
$result = $ffi->some_heavy_stuff(1000,1000);
echo "The heavy result: $result\n";
?>

13
php/heavy_stuff_opt_c.php Normal file
View file

@ -0,0 +1,13 @@
<?php
$ffi = FFI::cdef("
unsigned long lesser_heavy_stuff(int lima, int limb);",
"./heavy_stuff.so"
);
echo "Hello, less heavy stuff here:\n";
$result = $ffi->lesser_heavy_stuff(1000,1000);
echo "The heavy result: $result\n";
?>

16
php/lesser_heavy.php Normal file
View file

@ -0,0 +1,16 @@
<?php
echo "Hello,not so heavy stuff here:\n";
function someReallyHeavyStuff($lima, $limb){
$result=0;
for($i=0;$i<$lima;$i++){
$result+=($i*$limb)+(($limb-1)*$limb)/2;
}
return $result;
}
$result = someReallyHeavyStuff(100000,100000);
echo "The not so heavy result: $result\n"
?>

BIN
php/main Executable file

Binary file not shown.

12
php/main.c Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
extern unsigned long some_heavy_stuff(int lima, int limb);
int main(void) {
int lima = 1000;
int limb = 1000;
printf("Hello from blistering C: \n");
printf("Doing stuff: \n");
unsigned long result = some_heavy_stuff(lima, limb);
printf("Result is %lu", result);
}