Skip to content

Instantly share code, notes, and snippets.

@bogvsdev
Last active September 30, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogvsdev/9352b3b777abfd34aa5a72bb0e8b6a30 to your computer and use it in GitHub Desktop.
Save bogvsdev/9352b3b777abfd34aa5a72bb0e8b6a30 to your computer and use it in GitHub Desktop.
Execution time comparison

Execution time comparison of php5.6, php7.1 and golang

Due to my new project requirements decided to test performance of php vs golang. Subjects of test are:

  • O(n^2) function
  • mysql connection with select query using aggregation function AVG()

Results

  • php5.6 - 435.51 sec
  • php7.1 - 187.32 sec
  • golang - 5.46 sec
package main
import (
"time"
"fmt"
_ "github.com/go-sql-driver/mysql"
"database/sql"
)
func main() {
start := time.Now()
c := make(chan int)
go s(c)
nums := <-c
c2 := make(chan string)
go s2(c2)
name := <-c2
fmt.Printf("Function s result: %d \n", nums)
fmt.Printf("Function s2 result: %s \n", name)
elapsed := time.Since(start)
fmt.Printf("This script took %s to execute \n", elapsed)
}
func s(c chan int) {
s := 0
for i := 1; i<100000; i++ {
for j := 1; j<100000; j++ {
s += (j*i)
}
}
c <- s
}
func s2(c chan string) {
db, _ := sql.Open("mysql", "root:docker@tcp(127.0.0.1:3307)/sys")
rows, _ := db.Query("SELECT AVG(total) as tot FROM io_by_thread_by_latency")
for rows.Next() {
var tot string
_ = rows.Scan(&tot)
c <- tot
}
}
<?php
$executionStartTime = microtime(true);
function s()
{
$s = 0;
for ($i = 1; $i < 100000; $i++) {
for ($j = 1; $j < 100000; $j++) {
$s += ($j*$i);
}
}
return $s;
}
function s2()
{
$db = new PDO('mysql:host=127.0.0.1;port=3307;dbname=sys;charset=utf8', 'root', 'docker');
$sql = "SELECT AVG(total) as tot FROM io_by_thread_by_latency";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows[0]['tot'];
}
echo "Function s result :" . s() ."\n";
echo "Function s2 result :" . s2() ."\n";
$executionEndTime = microtime(true);
$seconds = $executionEndTime - $executionStartTime;
echo "This script took $seconds to execute.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment