Skip to content

Instantly share code, notes, and snippets.

@rizkyabdilah
Created January 15, 2012 15:31
Show Gist options
  • Save rizkyabdilah/1616179 to your computer and use it in GitHub Desktop.
Save rizkyabdilah/1616179 to your computer and use it in GitHub Desktop.
Compare image downloader speed in Go, python and php.
package main
import (
"http"
"io/ioutil"
"fmt"
"os"
)
//var (
// ErrNotImage = os.NewError("response content type bukan gambar")
//)
func main(){
for i := 1; i <= 100; i++ {
err := Download(fmt.Sprintf("http://localhost/1.jpg?nocache=%d", i), fmt.Sprintf("/tmp/%d.jpg", i))
if err != nil {
panic(err)
}
}
fmt.Printf("done\n")
}
func Download(url, fileName string) (os.Error) {
content, err := GetImage(url)
if err != nil {
return err
}
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
file.Write(content)
return nil
}
func GetImage(url string) ([]byte, os.Error) {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
//contentType := resp.Header.Get("content-type")
//if contentType[0:5] != "image" {
// return []uint8("bukan gambar"), ErrNotImage
//}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return content, nil
}
<?php
for ($i = 1; $i <= 100; $i++){
$content = file_get_contents('http://localhost/1.jpg?nocache='.$i);
$fp = fopen('/tmp/'.$i.'.jpg', 'wb');
fwrite($fp, $content);
fclose($fp);
}
echo "done";
#!/usr/bin/python
import urllib2
import shutil
for i in range(100):
content = urllib2.urlopen(urllib2.Request('http://localhost/1.jpg?nocache=%d' % i))
with open('/tmp/%d.jpg' % i, 'wb') as fp:
shutil.copyfileobj(content, fp)
content.close()
print "done"

Prerequisite

OS: Linux Ubuntu 11.10
Arch: x86_64
go rev: c1702f36df03 (release-branch.r60) release/release.r60.3
WebServer to serve image: Nginx

Go

abdilah@abdilah-komputer:/opt/workspace/trygo$ 6g picture-grabber.go && 6l -o picture-grabber-bin picture-grabber.6g
abdilah@abdilah-komputer:/opt/workspace/trygo$ time ./picture-grabber-bin 
done

real	0m0.102s
user	0m0.068s
sys	0m0.024s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time ./picture-grabber-bin 
done

real	0m0.111s
user	0m0.036s
sys	0m0.052s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time ./picture-grabber-bin 
done

real	0m0.105s
user	0m0.068s
sys	0m0.028s

PHP

abdilah@abdilah-komputer:/opt/workspace/trygo$ time php ./picture-grabber.php 
done
real	0m0.120s
user	0m0.040s
sys	0m0.032s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time php ./picture-grabber.php 
done
real	0m0.124s
user	0m0.048s
sys	        0m0.056s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time php ./picture-grabber.php 
done
real	0m0.079s
user	0m0.024s
sys	        0m0.044s

Python

abdilah@abdilah-komputer:/opt/workspace/trygo$ time python picture-grabber.py 
done

real	0m0.175s
user	0m0.088s
sys	        0m0.076s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time python picture-grabber.py 
done

real	0m0.167s
user	0m0.116s
sys    	0m0.044s
abdilah@abdilah-komputer:/opt/workspace/trygo$ time python picture-grabber.py 
done

real	0m0.164s
user	0m0.108s
sys	        0m0.048s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment