From 920ce3b4499ef0b9e2ec98e859dc41827ddb552e Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 8 Sep 2022 22:56:41 +0800 Subject: [PATCH] =?UTF-8?q?test[go]:=20go=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocol/src/test/go/benchmark1.go | 70 ++++++++++++++++++++++++++++++ protocol/src/test/go/benchmark2.go | 54 +++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 protocol/src/test/go/benchmark1.go create mode 100644 protocol/src/test/go/benchmark2.go diff --git a/protocol/src/test/go/benchmark1.go b/protocol/src/test/go/benchmark1.go new file mode 100644 index 00000000..d5b56eb5 --- /dev/null +++ b/protocol/src/test/go/benchmark1.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + // 预热 + test() + // 正式执行 + test() +} + +func test() { + var startTime = time.Now() + var arr = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} + const NUM int = 100000000 + for i := 0; i < NUM; i++ { + bubbleSort(arr) + } + fmt.Println(time.Since(startTime).Milliseconds()) +} + +//排序 +func bubbleSort(arr []int) { + for j := 0; j < len(arr)-1; j++ { + for k := 0; k < len(arr)-1-j; k++ { + if arr[k] < arr[k+1] { + temp := arr[k] + arr[k] = arr[k+1] + arr[k+1] = temp + } + } + } +} + +/** +public class MainTest { + public static void main(String[] args) { + // java的jit预热 + test(); + // 正式执行 + test(); + } + + static void test() { + // 正式执行 + var startTime = System.currentTimeMillis(); + var array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; + for (var i = 0; i < 100000000; i++) { + bubbleSort(array); + } + var endTime = System.currentTimeMillis(); + System.out.println(endTime - startTime); + } + + public static void bubbleSort(int[] arr) { + for (int i = 0; i < arr.length - 1; i++) { + for (int j = 0; j < arr.length - 1 - i; j++) { + if (arr[j] < arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } +} + */ diff --git a/protocol/src/test/go/benchmark2.go b/protocol/src/test/go/benchmark2.go new file mode 100644 index 00000000..a9cfd2a5 --- /dev/null +++ b/protocol/src/test/go/benchmark2.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + // 预热 + test() + // 正式执行 + test() +} + +func test() { + var startTime = time.Now() + for i := 0; i < 10000; i++ { + fibonacci(32) + } + fmt.Println(time.Since(startTime).Milliseconds()) +} + +func fibonacci(i int) int { + if i < 2 { + return i + } + return fibonacci(i-2) + fibonacci(i-1) +} + +/** +public class MainTest { + public static void main(String[] args) { + // java的jit预热 + test(); + // 正式执行 + test(); + } + + static void test() { + // 正式执行 + var startTime = System.currentTimeMillis(); + for (int i = 0; i < 10000; i++) { + fibonacci(32); + } + var endTime = System.currentTimeMillis(); + System.out.println(endTime - startTime); + } + + static int fibonacci(int i) { + if (i < 2) return i; + return fibonacci(i - 2) + fibonacci(i - 1); + } +} + */