diff --git a/test/benchmarks.cc b/test/benchmarks.cc index 07326735..1935f431 100644 --- a/test/benchmarks.cc +++ b/test/benchmarks.cc @@ -19,8 +19,27 @@ vector language_names({ "python", }); +size_t mean(const vector &values) { + if (values.empty()) return 0; + double result = 0; + for (double value : values) { + result += value; + } + return result / values.size(); +} + +size_t min(const vector &values) { + double result = 0; + for (double value : values) { + if (value < result || result == 0) result = value; + } + return result; +} + int main(int argc, char *arg[]) { map> example_entries_by_language_name; + vector error_speeds; + vector non_error_speeds; auto document = ts_document_new(); @@ -46,6 +65,11 @@ int main(int argc, char *arg[]) { for (auto &example : example_entries_by_language_name[language_name]) { if (file_name_filter && example.file_name != file_name_filter) continue; + if (example.input.size() < 256) continue; + + ts_document_invalidate(document); + ts_document_set_input_string(document, ""); + ts_document_parse(document); ts_document_invalidate(document); ts_document_set_input_string(document, example.input.c_str()); @@ -55,7 +79,9 @@ int main(int argc, char *arg[]) { clock_t end_time = clock(); unsigned duration = (end_time - start_time) * 1000 / CLOCKS_PER_SEC; assert(!ts_node_has_error(ts_document_root_node(document))); - printf(" %-30s\t%u\n", example.file_name.c_str(), duration); + size_t speed = static_cast(example.input.size()) / duration; + printf(" %-30s\t%u ms\t\t%lu bytes/ms\n", example.file_name.c_str(), duration, speed); + non_error_speeds.push_back(speed); } for (auto &other_language_name : language_names) { @@ -63,6 +89,7 @@ int main(int argc, char *arg[]) { for (auto &example : example_entries_by_language_name[other_language_name]) { if (file_name_filter && example.file_name != file_name_filter) continue; + if (example.input.size() < 256) continue; ts_document_invalidate(document); ts_document_set_input_string(document, example.input.c_str()); @@ -71,12 +98,23 @@ int main(int argc, char *arg[]) { ts_document_parse(document); clock_t end_time = clock(); unsigned duration = (end_time - start_time) * 1000 / CLOCKS_PER_SEC; - printf(" %-30s\t%u\n", example.file_name.c_str(), duration); + size_t speed = static_cast(example.input.size()) / duration; + printf(" %-30s\t%u ms\t\t%lu bytes/ms\n", example.file_name.c_str(), duration, speed); + error_speeds.push_back(speed); } } puts(""); } + puts("without errors:"); + printf(" %-30s\t%lu bytes/ms\n", "average speed", mean(non_error_speeds)); + printf(" %-30s\t%lu bytes/ms\n", "worst speed", min(non_error_speeds)); + puts(""); + + puts("with errors:"); + printf(" %-30s\t%lu bytes/ms\n", "average speed", mean(error_speeds)); + printf(" %-30s\t%lu bytes/ms\n", "worst speed", min(error_speeds)); + return 0; }