125 lines
3.6 KiB
C
Raw Normal View History

#include <benchmark/benchmark.h>
#include "test_common.h"
2022-01-04 12:39:04 +08:00
2022-01-04 00:29:11 +08:00
extern "C" {
#include "PikaMain.h"
2022-02-21 16:16:18 +08:00
#include "PikaParser.h"
2022-01-04 00:29:11 +08:00
#include "PikaStdLib_MemChecker.h"
2022-02-21 16:16:18 +08:00
#include "PikaVM.h"
2022-01-04 00:29:11 +08:00
#include "dataArgs.h"
#include "dataMemory.h"
2022-02-21 16:16:18 +08:00
#include "dataStrs.h"
2022-01-04 00:29:11 +08:00
#include "pikaScript.h"
}
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-03-14 21:24:35 +08:00
pikaVM_run(pikaMain, (char *)
2022-01-04 09:33:24 +08:00
"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 09:35:58 +08:00
static void while_loop_10000(benchmark::State& state) {
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
2022-03-14 21:24:35 +08:00
pikaVM_run(pikaMain, (char *)
2022-01-04 09:35:58 +08:00
"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-10-09 00:05:48 +08:00
void __platform_printf(char* fmt, ...) {}
static void for_print_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = Parser_linesToAsm(buffs, (char*)
"for i in range(1000):\n"
" print(i)\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(for_print_1000)->Unit(benchmark::kMillisecond);
2022-02-10 23:39:43 +08:00
static void prime_number_100(benchmark::State& state) {
2022-01-04 11:24:34 +08:00
int num = 0;
2022-02-21 16:16:18 +08:00
Args* buffs = New_strBuff();
char* pikaAsm = Parser_linesToAsm(buffs, (char*)
2022-01-04 11:24:34 +08:00
"num = 0\n"
2022-10-05 20:35:04 +08:00
"i = 2\n"
2022-09-23 23:00:36 +08:00
"while i < 100:\n"
2022-01-04 11:24:34 +08:00
" is_prime = 1\n"
2022-10-05 20:35:04 +08:00
" j = 2\n"
2022-09-23 23:00:36 +08:00
" while j < 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"
2022-10-05 20:35:04 +08:00
" j += 1 \n"
2022-01-04 11:24:34 +08:00
" if is_prime:\n"
" num = num + i\n"
2022-10-05 20:35:04 +08:00
" i += 1\n"
2022-01-04 11:24:34 +08:00
"\n");
2022-03-14 12:34:02 +08:00
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
2022-02-21 16:16:18 +08:00
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
2022-03-14 12:34:02 +08:00
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
2022-02-10 23:39:43 +08:00
num = obj_getInt(pikaMain, (char*)"num");
if (1060 != num) {
2022-08-16 11:05:19 +08:00
printf("Error: prime_number_100\r\n");
2022-01-04 13:03:01 +08:00
}
2022-02-10 23:39:43 +08:00
obj_deinit(pikaMain);
2022-02-10 22:58:44 +08:00
}
2022-03-14 12:34:02 +08:00
byteCodeFrame_deinit(&bytecode_frame);
2022-02-21 16:16:18 +08:00
args_deinit(buffs);
2022-02-10 22:58:44 +08:00
}
2022-02-10 23:39:43 +08:00
BENCHMARK(prime_number_100)->Unit(benchmark::kMillisecond);
2022-02-10 22:58:44 +08:00
static void prime_number_100_c(benchmark::State& state) {
2022-02-10 23:39:43 +08:00
int num = 0;
2022-02-10 22:58:44 +08:00
for (auto _ : state) {
2022-02-10 23:39:43 +08:00
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) {
2022-08-16 11:05:19 +08:00
printf("Error: prime_number_100\r\n");
2022-02-10 23:39:43 +08:00
}
2022-02-10 22:58:44 +08:00
}
2022-01-04 13:03:01 +08:00
}
2022-02-10 23:39:43 +08:00
BENCHMARK(prime_number_100_c)->Unit(benchmark::kMillisecond);
2022-01-25 09:15:14 +08:00
2022-09-23 23:00:36 +08:00
BENCHMARK_MAIN();