Skip to content

Commit

Permalink
wordlister: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhngtu committed Jan 14, 2024
1 parent 5646966 commit dcf868f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
76 changes: 76 additions & 0 deletions WordLister/Bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: GPL-3.0-only

#include "stdafx.h"
#include <chrono>
#include "Telex.h"
#include "WordListIterator.hpp"
#include "FileUtil.hpp"

using namespace VietType::Telex;
using namespace VietType::TestLib;

#ifdef _DEBUG
#define EITERATIONS 10
#define VITERATIONS 200
#else
#define EITERATIONS 100
#define VITERATIONS 2000
#endif

bool bench() {
{
LONGLONG efsize;
auto ewords = static_cast<wchar_t*>(ReadWholeFile(L"..\\..\\data\\ewdsw.txt", &efsize));
auto ewend = ewords + efsize / sizeof(wchar_t);
TelexConfig config;
TelexEngine engine(config);
unsigned long long count = 0;
auto t1 = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < EITERATIONS; i++) {
for (WordListIterator ew(ewords, ewend); ew != ewend; ew++) {
std::wstring eword(*ew, ew.wlen());
engine.Reset();
for (auto c : eword) {
engine.PushChar(c);
engine.Peek();
}
engine.Commit();
engine.Retrieve();
count++;
}
}
auto t2 = std::chrono::high_resolution_clock::now();
wprintf(
L"ewords total iters: %d, count = %llu, time = %llu us\n",
EITERATIONS,
count,
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
FreeFile(ewords);
}

{
LONGLONG vfsize;
auto vwords = static_cast<wchar_t*>(ReadWholeFile(L"..\\..\\data\\vw39kw.txt", &vfsize));
auto vwend = vwords + vfsize / sizeof(wchar_t);
TelexConfig config;
TelexEngine engine(config);
unsigned long long count = 0;
auto t1 = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < VITERATIONS; i++) {
for (WordListIterator vw(vwords, vwend); vw != vwend; vw++) {
std::wstring vword(*vw, vw.wlen());
engine.Reset();
engine.Backconvert(vword);
count++;
}
}
auto t2 = std::chrono::high_resolution_clock::now();
wprintf(
L"vwords total iters: %d, count = %llu, time = %llu us\n",
VITERATIONS,
count,
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
FreeFile(vwords);
}
return true;
}
6 changes: 5 additions & 1 deletion WordLister/WordLister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
bool vietscan(const wchar_t* filename);
bool engscan(const wchar_t* filename);
bool dualscan();
bool bench();

int wmain(int argc, wchar_t** argv) {
if (argc == 3 && !wcscmp(argv[1], L"vietscan")) {
Expand All @@ -13,10 +14,13 @@ int wmain(int argc, wchar_t** argv) {
return !engscan(argv[2]);
} else if (argc == 2 && !wcscmp(argv[1], L"dualscan")) {
return !dualscan();
} else if (argc == 2 && !wcscmp(argv[1], L"bench")) {
return !bench();
} else {
wprintf(L"usage: \n"
L" wordlister <vietscan|engscan> <filename>\n"
L" wordlister dualscan\n");
L" wordlister dualscan\n"
L" wordlister bench\n");
return 1;
}
}
1 change: 1 addition & 0 deletions WordLister/WordLister.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Bench.cpp" />
<ClCompile Include="DualScan.cpp" />
<ClCompile Include="EngScan.cpp" />
<ClCompile Include="VietScan.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions WordLister/WordLister.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<ClCompile Include="DualScan.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Bench.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
Expand Down

0 comments on commit dcf868f

Please sign in to comment.