Skip to content

Commit

Permalink
[8_13] improve performance when calculating hash of string
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored Jun 8, 2024
1 parent d933682 commit 85ba510
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Kernel/Types/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ operator<= (string s1, string s2) {

int
hash (string s) {
int i, h= 0, n= N (s);
for (i= 0; i < n; i++) {
int h= 0;
for (char ch : s) {
h= (h << 9) + (h >> 23);
h= h + ((int) s[i]);
h= h + ((int) ch);
}
return h;
}
Expand Down
8 changes: 8 additions & 0 deletions bench/Kernel/Types/string_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ main () {
static string a ("abc"), b ("de");
a << b;
});
bench.run ("hash of string", [&] {
static string a ("accde");
hash (a);
});
bench.run ("hash of larger string", [&] {
static string a ("compare larger string ,compute hash of LARGER string");
hash (a);
});
bench.run ("is quoted", [&] {
static string a ("H\"ello TeXmacs\"");
is_quoted (a);
Expand Down

0 comments on commit 85ba510

Please sign in to comment.