2022-01-04 00:16:43 +08:00
|
|
|
#include <benchmark/benchmark.h>
|
2022-01-04 00:29:11 +08:00
|
|
|
#include "gtest/gtest.h"
|
2022-01-04 12:39:04 +08:00
|
|
|
|
2022-01-04 00:29:11 +08:00
|
|
|
extern "C" {
|
|
|
|
#include "PikaMain.h"
|
|
|
|
#include "PikaStdLib_MemChecker.h"
|
|
|
|
#include "dataArgs.h"
|
|
|
|
#include "dataMemory.h"
|
|
|
|
#include "pikaScript.h"
|
|
|
|
}
|
2022-01-04 00:16:43 +08:00
|
|
|
|
2022-01-04 09:12:32 +08:00
|
|
|
static void for_loop_10000(benchmark::State& state) {
|
2022-01-04 00:29:11 +08:00
|
|
|
for (auto _ : state) {
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2022-01-04 09:33:24 +08:00
|
|
|
obj_run(pikaMain, (char *)
|
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10000):\n"
|
|
|
|
" a = a + 1\n"
|
|
|
|
"\n");
|
2022-01-25 09:15:14 +08:00
|
|
|
obj_deinit(pikaMain);
|
2022-01-04 00:29:11 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 12:09:59 +08:00
|
|
|
BENCHMARK(for_loop_10000)->Unit(benchmark::kMillisecond);
|
2022-01-04 00:16:43 +08:00
|
|
|
|
2022-01-04 09:35:58 +08:00
|
|
|
static void while_loop_10000(benchmark::State& state) {
|
|
|
|
for (auto _ : state) {
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char *)
|
|
|
|
"i = 0\n"
|
|
|
|
"while i < 10000:\n"
|
|
|
|
" i = i + 1\n"
|
|
|
|
"\n");
|
2022-01-25 09:15:14 +08:00
|
|
|
obj_deinit(pikaMain);
|
2022-01-04 09:35:58 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 12:09:59 +08:00
|
|
|
BENCHMARK(while_loop_10000)->Unit(benchmark::kMillisecond);
|
2022-01-04 09:35:58 +08:00
|
|
|
|
2022-01-04 11:24:34 +08:00
|
|
|
static void prime_number_100(benchmark::State& state) {
|
|
|
|
int num = 0;
|
|
|
|
for (auto _ : state) {
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char *)
|
|
|
|
"num = 0\n"
|
|
|
|
"i = 2\n"
|
|
|
|
"for i in range(2,100):\n"
|
|
|
|
" j=2\n"
|
|
|
|
" is_prime = 1\n"
|
|
|
|
" for j in range(2,i):\n"
|
2022-01-04 11:26:32 +08:00
|
|
|
" if i%j==0 :\n"
|
2022-01-04 11:24:34 +08:00
|
|
|
" is_prime = 0\n"
|
|
|
|
" break\n"
|
|
|
|
" if is_prime:\n"
|
|
|
|
" num = num + i\n"
|
|
|
|
"\n");
|
|
|
|
num = obj_getInt(pikaMain, (char*)"num");
|
|
|
|
if (1060 != num) {
|
|
|
|
printf("[error]: prime_number_100\r\n");
|
|
|
|
}
|
2022-01-25 09:15:14 +08:00
|
|
|
obj_deinit(pikaMain);
|
2022-01-04 11:24:34 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 12:09:59 +08:00
|
|
|
BENCHMARK(prime_number_100)->Unit(benchmark::kMillisecond);
|
2022-01-04 09:35:58 +08:00
|
|
|
|
2022-01-04 13:03:01 +08:00
|
|
|
|
|
|
|
static void prime_number_100_c(benchmark::State& state) {
|
|
|
|
int num = 0;
|
|
|
|
for (auto _ : state) {
|
|
|
|
num = 0;
|
|
|
|
/* run */
|
|
|
|
for (int i = 2; i < 100; i++) {
|
|
|
|
int is_prime = 1;
|
|
|
|
for (int j = 2; j < i; j++) {
|
|
|
|
if (i % j == 0) {
|
|
|
|
is_prime = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_prime) {
|
|
|
|
num = num + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (1060 != num) {
|
|
|
|
printf("[error]: prime_number_100\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 09:15:14 +08:00
|
|
|
BENCHMARK(prime_number_100_c)->Unit(benchmark::kMillisecond);
|
|
|
|
|
|
|
|
BENCHMARK_MAIN();
|