Skip to content

Instantly share code, notes, and snippets.

@jayachandraoggy
Last active March 26, 2024 08:42
Show Gist options
  • Save jayachandraoggy/c284cfe1aaba10f295172e21d3ec514c to your computer and use it in GitHub Desktop.
Save jayachandraoggy/c284cfe1aaba10f295172e21d3ec514c to your computer and use it in GitHub Desktop.

govsphp

Simple demonstration to compare go and php language execution time by using for loop with one billion times loop.

CPU: Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz


go program result 1.428860546s

with go compiler: go version go1.22.0 linux/amd64

package main

import (
	"fmt"
	"time"
)

func main() {
	var n int = 0
	now := time.Now()
	defer func() {
		fmt.Println(n)
		fmt.Println(time.Now().Sub(now))
	}()

	for i := 0; i < 1000000000; i++ {
		n += 1
	}
}

php program result 6.4896709918976 Secs

with PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)

<?php

$timeStart = microtime(true);

$n = 0;
for ($i = 1; $i <= 1000000000; $i++) {
    $n += 1;
}

$timeEnd = microtime(true);

echo $n . "\n";
echo ($timeEnd - $timeStart) . " Secs\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment