|
3 | 3 |
|
4 | 4 | // NOLINTNEXTLINE |
5 | 5 | static void BenchmarkRadd(benchmark::State &state) { |
6 | | - // NOLINTNEXTLINE |
7 | | - for (auto _ : state) { |
| 6 | + for (const auto &_ : state) { |
8 | 7 | jucc::Radd(0, 1000000); |
9 | 8 | } |
10 | 9 | } |
11 | 10 |
|
12 | 11 | BENCHMARK(BenchmarkRadd); |
| 12 | + |
| 13 | +// NOLINTNEXTLINE |
| 14 | +static void BenchmarkJuCC(benchmark::State &state) { |
| 15 | + std::string file_grammar = "../benchmark/input/benchmark_grammar.g"; |
| 16 | + std::string file_input = "../benchmark/input/in1.cc"; |
| 17 | + for (const auto &_ : state) { |
| 18 | + jucc::grammar::Parser grammar_parser = jucc::grammar::Parser(file_grammar.c_str()); |
| 19 | + grammar_parser.Parse(); |
| 20 | + jucc::grammar::Productions raw_productions = grammar_parser.GetProductions(); |
| 21 | + jucc::grammar::Productions productions = jucc::utils::RemoveAllPossibleAmbiguity(raw_productions); |
| 22 | + auto nullables = jucc::utils::CalcNullables(productions); |
| 23 | + auto firsts = jucc::utils::CalcFirsts(productions, nullables); |
| 24 | + auto follows = jucc::utils::CalcFollows(productions, firsts, nullables, grammar_parser.GetStartSymbol()); |
| 25 | + auto terminals = grammar_parser.GetTerminals(); |
| 26 | + auto non_terminals = jucc::utils::GetAllNonTerminals(productions); |
| 27 | + jucc::parser::ParsingTable parsing_table = jucc::parser::ParsingTable(terminals, non_terminals); |
| 28 | + parsing_table.SetFirsts(firsts); |
| 29 | + parsing_table.SetFollows(follows); |
| 30 | + parsing_table.SetProductions(productions); |
| 31 | + parsing_table.BuildTable(); |
| 32 | + |
| 33 | + std::ifstream ifs(file_input); |
| 34 | + std::vector<std::string> input_tokens; |
| 35 | + jucc::lexer::Lexer lexer = jucc::lexer::Lexer(); |
| 36 | + int token; |
| 37 | + while ((token = lexer.GetToken(ifs)) != jucc::lexer::TOK_EOF) { |
| 38 | + std::string ret_string = jucc::lexer::Lexer::GetTokenType(token); |
| 39 | + if (ret_string == "ignore") { |
| 40 | + continue; |
| 41 | + } |
| 42 | + input_tokens.emplace_back(ret_string); |
| 43 | + } |
| 44 | + |
| 45 | + jucc::parser::Parser parser = jucc::parser::Parser(); |
| 46 | + parser.SetInputString(input_tokens); |
| 47 | + parser.SetStartSymbol(grammar_parser.GetStartSymbol()); |
| 48 | + parser.SetParsingTable(parsing_table); |
| 49 | + while (!parser.IsComplete()) { |
| 50 | + parser.ParseNextStep(); |
| 51 | + } |
| 52 | + parser.BuildParseTree(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +BENCHMARK(BenchmarkJuCC); |
| 57 | + |
| 58 | +/* |
| 59 | + * Same as BenchmarkJuCC but without disk operations |
| 60 | + */ |
| 61 | +// NOLINTNEXTLINE |
| 62 | +static void BenchmarkJuCCCompute(benchmark::State &state) { |
| 63 | + std::string file_grammar = "../benchmark/input/benchmark_grammar.g"; |
| 64 | + std::string file_input = "../benchmark/input/in1.cc"; |
| 65 | + |
| 66 | + jucc::grammar::Parser grammar_parser = jucc::grammar::Parser(file_grammar.c_str()); |
| 67 | + grammar_parser.Parse(); |
| 68 | + |
| 69 | + std::ifstream ifs(file_input); |
| 70 | + std::vector<std::string> input_tokens; |
| 71 | + jucc::lexer::Lexer lexer = jucc::lexer::Lexer(); |
| 72 | + int token; |
| 73 | + while ((token = lexer.GetToken(ifs)) != jucc::lexer::TOK_EOF) { |
| 74 | + std::string ret_string = jucc::lexer::Lexer::GetTokenType(token); |
| 75 | + if (ret_string == "ignore") { |
| 76 | + continue; |
| 77 | + } |
| 78 | + input_tokens.emplace_back(ret_string); |
| 79 | + } |
| 80 | + |
| 81 | + for (const auto &_ : state) { |
| 82 | + jucc::grammar::Productions raw_productions = grammar_parser.GetProductions(); |
| 83 | + jucc::grammar::Productions productions = jucc::utils::RemoveAllPossibleAmbiguity(raw_productions); |
| 84 | + auto nullables = jucc::utils::CalcNullables(productions); |
| 85 | + auto firsts = jucc::utils::CalcFirsts(productions, nullables); |
| 86 | + auto follows = jucc::utils::CalcFollows(productions, firsts, nullables, grammar_parser.GetStartSymbol()); |
| 87 | + auto terminals = grammar_parser.GetTerminals(); |
| 88 | + auto non_terminals = jucc::utils::GetAllNonTerminals(productions); |
| 89 | + jucc::parser::ParsingTable parsing_table = jucc::parser::ParsingTable(terminals, non_terminals); |
| 90 | + parsing_table.SetFirsts(firsts); |
| 91 | + parsing_table.SetFollows(follows); |
| 92 | + parsing_table.SetProductions(productions); |
| 93 | + parsing_table.BuildTable(); |
| 94 | + |
| 95 | + jucc::parser::Parser parser = jucc::parser::Parser(); |
| 96 | + parser.SetInputString(input_tokens); |
| 97 | + parser.SetStartSymbol(grammar_parser.GetStartSymbol()); |
| 98 | + parser.SetParsingTable(parsing_table); |
| 99 | + while (!parser.IsComplete()) { |
| 100 | + parser.ParseNextStep(); |
| 101 | + } |
| 102 | + parser.BuildParseTree(); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +BENCHMARK(BenchmarkJuCCCompute); |
0 commit comments