Skip to content

Instantly share code, notes, and snippets.

@zbraniecki
Last active September 19, 2019 17:06
Show Gist options
  • Save zbraniecki/016f7bd35fc6e09aede997c5bc20222a to your computer and use it in GitHub Desktop.
Save zbraniecki/016f7bd35fc6e09aede997c5bc20222a to your computer and use it in GitHub Desktop.
Performance of ICU Locale parsing vs. unic-locale proposal
#include <stdio.h>
#include <chrono>
#include <vector>
#include <cstring>
#include "unicode/uloc.h"
std::vector<std::string> ids;
ids.push_back("en-US");
ids.push_back("en-GB");
ids.push_back("es-AR");
ids.push_back("it");
ids.push_back("zh-Hans-CN");
ids.push_back("de-AT");
ids.push_back("pl");
ids.push_back("fr-FR");
ids.push_back("de-AT");
ids.push_back("sr-Cyrl-SR");
ids.push_back("nb-NO");
ids.push_back("fr-FR");
ids.push_back("mk");
ids.push_back("uk");
ids.push_back("und-PL");
ids.push_back("und-Latn-AM");
ids.push_back("ug-Cyrl");
ids.push_back("sr-ME");
ids.push_back("mn-Mong");
ids.push_back("lif-Limb");
ids.push_back("gan");
ids.push_back("zh-Hant");
ids.push_back("yue-Hans");
ids.push_back("unr");
ids.push_back("unr-Deva");
ids.push_back("und-Thai-CN");
ids.push_back("ug-Cyrl");
ids.push_back("en-Latn-DE");
ids.push_back("pl-FR");
ids.push_back("de-CH");
ids.push_back("tuq");
ids.push_back("sr-ME");
ids.push_back("ng");
ids.push_back("klx");
ids.push_back("kk-Arab");
ids.push_back("en-Cyrl");
ids.push_back("und-Cyrl-UK");
ids.push_back("und-Arab");
ids.push_back("und-Arab-FO");
auto start = std::chrono::steady_clock::now();
for (auto& id : ids) {
const int32_t kLocaleMax = 160;
char maxLocale[kLocaleMax];
UErrorCode status = U_ZERO_ERROR;
uloc_addLikelySubtags(id.c_str(), maxLocale, kLocaleMax, &status);
nsDependentCString maxLocStr(maxLocale);
// printf("For string %s, result is %s.\n", id.c_str(), maxLocale);
}
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
auto diff_sec = std::chrono::duration_cast<std::chrono::nanoseconds>(diff);
printf("Create Locale from str time: %d ns\n", diff_sec);
#include <unicode/ures.h>
#include <unicode/putil.h>
#include <unicode/ustdio.h>
#include <unicode/locid.h>
#include <unicode/localebuilder.h>
#include <stdio.h>
#include <chrono>
#include <vector>
#include <cstring>
#ifndef _UASSERT
#define _UASSERT
#define ASSERT_OK(x) if(U_FAILURE(x)) { fprintf(stderr,"%s:%d: ICU Failure %s\n", __FILE__, __LINE__, u_errorName(x)); }
#endif
static void show(void);
void show(void)
{
UErrorCode status = U_ZERO_ERROR;
{
std::vector<std::string> ids;
ids.push_back("en-US");
ids.push_back("en-GB");
ids.push_back("es-AR");
ids.push_back("it");
ids.push_back("zh-Hans-CN");
ids.push_back("de-AT");
ids.push_back("pl");
ids.push_back("fr-FR");
ids.push_back("de-AT");
ids.push_back("sr-Cyrl-SR");
ids.push_back("nb-NO");
ids.push_back("fr-FR");
ids.push_back("mk");
ids.push_back("uk");
auto start = std::chrono::steady_clock::now();
for (auto& id : ids) {
icu_64::Locale aLocale = icu_64::Locale(id.c_str());
}
auto end = std::chrono::steady_clock::now();
if(U_FAILURE(status)) {
printf("Can't open resource bundle. Error is %s\n", u_errorName(status));
return;
}
auto diff = end - start;
auto diff_sec = std::chrono::duration_cast<std::chrono::nanoseconds>(diff);
printf("Create Locale from str time: %d ns\n", diff_sec);
}
}
int main() {
show();
return 0;
}
On Thinkpad Carbon X1 gen 5, Arch Linux:
C++ code based on `language_identifier_from_str` benchmark in `unic-langid` crate.
Create Locale from str time:
C++ (gcc 8.3.): 68605 ns
Rust (Rust 1.34): 1268 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment