funcmain() { fileName := "Data.txt" data := make([]int, 0, 50000) start := time.Now() fi, err := os.Open(fileName) if err != nil { fmt.Printf("Error: %s\n", err) return } defer fi.Close() br := bufio.NewReader(fi) for { a, _, c := br.ReadLine() if c == io.EOF { break } num, _ := strconv.Atoi(string(a)) data = append(data, num) } endReadTime := time.Now() fmt.Printf("Read time: %fs\n", endReadTime.Sub(start).Seconds()) BubbleSort(data) endTime := time.Now() fmt.Printf("Sort time: %fs\n", endTime.Sub(endReadTime).Seconds()) fmt.Printf("finished time: %fs\n", endTime.Sub(start).Seconds()) }
funcBubbleSort(arr []int) { length := len(arr) var flag int var tmp int for i := 0; i < length; i++ { flag = 0 for j := 1; j < (length - i); j++ { if arr[j] < arr[j-1] { tmp = arr[j] arr[j] = arr[j-1] arr[j-1] = tmp flag = 1 } } if flag == 0 { return } } }
~/Workspaces/go » go run main.go cost time: 0.952737 ------------------------------------------------------- ~/Workspaces/go » go run main.go cost time: 0.983901 ------------------------------------------------------- ~/Workspaces/go » go run main.go cost time: 0.967799 ------------------------------------------------------- ~/Workspaces/go » go run main.go cost time: 0.972302 ------------------------------------------------------- ~/Workspaces/go » go run main.go cost time: 0.980509
res_data = urllib2.urlopen(req) res = res_data.read() return res t = time.time() for i in xrange(0, 20): # print "Get("+str(i)+"):" data = get("http://2018.ip138.com/ic.asp?count="+str(i)) # print data print'Python time: %.02fs' % (time.time() - t)
funcmain() { t1 := time.Now() for i := 0; i < 10000000; i++ { Aaa(float64(i)) } t2 := time.Now() fmt.Printf("Go time: %f s\n", t2.Sub(t1).Seconds()) }
funcAaa(i float64) { var a float64 = i + 1 var b float64 = 2.3 s := "abcdefkkbghisdfdfdsfds"
if a > b { a++ } else { b = b + 1 }
if a == b { b = b + 1 }
c := a*b + a/b - math.Pow(a, 2) d := s[0:strings.Index(s, "kkb")] + strconv.FormatFloat(c, 'E', -1, 64) _ = d }
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
~/Workspaces/GoLand/src/Aaa » go build main.go ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 4.083166 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 4.095651 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 4.154200 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 4.175203 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 4.111375 s
~/Workspaces/GoLand/src/Aaa » go build main.go ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 2.326077 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 2.270769 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 2.277345 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 2.252540 s ------------------------------------------------------- ~/Workspaces/GoLand/src/Aaa » ./main Go time: 2.255374 s
var t1 = (newDate()).getTime(); for(var i=0; i<10000000; i++){ aaa(i); } var t2 = (newDate()).getTime(); console.log("nodejs time:" + (t2 - t1) + "ms"); functionaaa(i){ var a = i + 1; var b = 2.3; var s = "abcdefkkbghisdfdfdsfds"; if(a > b){ ++a; }else{ b = b + 1; } if(a == b){ b = b + 1; } var c = a * b + a / b - Math.pow(a, 2); var d = s.substring(0, s.indexOf("kkb")) + c.toString(); }
import sys, time, math defaaa(i): a = i + 1 b = 2.3 s = "abcdefkkbghisdfdfdsfds" if a > b: a = a + 1 else: b = b + 1 if a == b: b = b + 1 c = a * b +a / b - math.pow(a, 2) d = s[0: s.find("kkb")] + str(c) t = time.time() for i in xrange(0, 10000000): aaa(i) print'Python time: %.02f s' % (time.time() - t)