GAE/Gが結構速いです。

Google App Engine with Golangのテスターになれたので、何かDeployしようとコード書いています。

そのついでに、ちょっとベンチマークしてGAE/Pythonと比較してみました。


とりあえず、二重ループで総当り素数判定(<10000)っぽいものをPythonとGoで書いて、両者をデプロイしてタイムを比較。
時間は処理している間だけね。

結果

 回数   Golang   Python 
1回目 49.2 807.3
2回目 49.7 833.0
3回目 49.0 830.6
4回目 50.0 806.7
5回目 49.2 782.2
平均 49.42 811.96

という感じになりました。ちなみに単位は[ms]です。
Golangが20倍近く速い感じですね。素晴らしいです。

ついでに使ったコードを晒しておきます。

Golang

package test4golang 
 
import ( 
    "fmt" 
    "http" 
    "os" 
    ) 
 
 
func init(){ 
    http.HandleFunc("/",prime) 
} 
 
func prime(w http.ResponseWriter, r *http.Request) { 
    startTimeS, startTimeM, _ := os.Time() 
    for i:=1;i<10000;i++ { 
        for j:=2; j<int(i/2); j++ { 
            if i%j == 0 { 
                break; 
            } 
        } 
    } 
    finishTimeS, finishTimeM, _ := os.Time() 
 
    finishTime := finishTimeS*1e9 + finishTimeM 
    startTime := startTimeS*1e9 + startTimeM 
 
    fmt.Fprintf(w,"%d , %d<br> %d",startTime,finishTime,finishTime-startTime) 
    return 
}

Python

import cgi

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from time import time

class PrimeNumber(webapp.RequestHandler):
    def get(self):
        i=0
        j=0
        startTime = time()
        while i<10000:
            j = 2
            while j<(i/2):
                if i%j == 0:
                    break
                j +=1
            i += 1
        finishTime = time()
        self.response.out.write(finishTime - startTime)

application = webapp.WSGIApplication([
                        ('/',PrimeNumber)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

訂正

6/11 14:56 Pythonの結果がボロボロに間違えていたので訂正しました。