This realization of Whirlpool works very slow. For example, i wrote a program, which hashing all files in current directory and printing result.
I used 2 variants of builds and compared them to PHP realization of same program.
Enviroment:
Intel Core2 Duo CPU E7500 @ 2.93GHz
3536 Mb DDR2
Ubuntu 12.04 with last updates
➜ uname -a
Linux pyxis-server 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 15:31:16 UTC 2013 i686 i686 i386 GNU/Linux
➜ go version
go version go1
➜ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"
➜ aptitude show golang
...
Version: 2:1-5
...
➜ php -v
PHP 5.3.10-1ubuntu3.11 with Suhosin-Patch (cli) (built: Apr 4 2014 01:27:23)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
The code:
package main
import (
"fmt"
"github.com/jzelinskie/whirlpool"
"io/ioutil"
"os"
"encoding/hex"
)
func main() {
dir, _ := os.Open("./") // current dir
list, _ := dir.Readdirnames(-1) // if ok err == nil
listsize := len(list)
var hashes = make([]string, listsize)
whi := whirlpool.New()
// bulding list of hashes
fmt.Println("listsize =", listsize)
for cn := 0; cn < listsize; cn++ {
file, _ := ioutil.ReadFile(list[cn])
whi.Write(file)
hashes[cn] = hex.EncodeToString(whi.Sum(nil))
fmt.Print(list[cn], " ", hashes[cn], "\n")
Reset()
}
}
Almost same for PHP:
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".")
&& ($file != ".."))
{
echo $file . " " . hash( "whirlpool", file_get_contents($file) ) . "\n";
}
}
closedir($handle);
}
?>
Results:
# Standart compiler
➜ go build ./dedup.go
➜ time ./dedup > /dev/null
real 0m4.612s
user 0m4.588s
sys 0m0.020s
# gccgo compiler
➜ go build -compiler gccgo ./dedup.go
➜ time ./dedup > /dev/null
real 0m2.110s
user 0m2.084s
sys 0m0.024s
# PHP realization
➜ time php hash.php > /dev/null
real 0m0.634s
user 0m0.608s
sys 0m0.024s
I've profiled the "go build" variant of program (same run time with profiling code) and saw, that 94.1% of time program was in
github.com/jzelinskie/whirlpool.(*whirlpool).transform
I understand, what Go realisation may be slower (PHP have it in module written is C, i suppose), but why the difference is so huge?
Is there any way to make it faster?