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()
- php5.6 - 435.51 sec
- php7.1 - 187.32 sec
- golang - 5.46 sec
Due to my new project requirements decided to test performance of php vs golang. Subjects of test are:
| 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"; |