Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <ctime>
- using namespace std;
- int
- glibc_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
- {
- wchar_t c1;
- wchar_t c2;
- while (n >= 4)
- {
- c1 = s1[0];
- c2 = s2[0];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- c1 = s1[1];
- c2 = s2[1];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- c1 = s1[2];
- c2 = s2[2];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- c1 = s1[3];
- c2 = s2[3];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- s1 += 4;
- s2 += 4;
- n -= 4;
- }
- if (n > 0)
- {
- c1 = s1[0];
- c2 = s2[0];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- ++s1;
- ++s2;
- --n;
- }
- if (n > 0)
- {
- c1 = s1[0];
- c2 = s2[0];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- ++s1;
- ++s2;
- --n;
- }
- if (n > 0)
- {
- c1 = s1[0];
- c2 = s2[0];
- if (c1 - c2 != 0)
- return c1 > c2 ? 1 : -1;
- }
- return 0;
- }
- int main(int argc, char ** argv) {
- const wchar_t my_wchar11[] = L"rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
- const wchar_t my_wchar22[] = L"rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
- const char my_char11[] = "rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
- const char my_char22[] = "rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
- //----------------------------------------------------------------------------------------
- const size_t num = 99999999;
- int clock1;
- int clock2;
- //------------------------------------------------------Сравнение через wmemcmp:-----------------------
- int status_wmemcmp = 0;
- const int wlen = wcslen(my_wchar22);
- clock1 = clock();
- for (size_t i = 0; i < num; i++)
- {
- const int pos = i % wlen;
- status_wmemcmp += wmemcmp(my_wchar11 + pos, my_wchar22 + pos, wlen - pos);
- }
- clock2 = clock();
- cout << "time_wmemcmp:" << clock2 - clock1 << endl;
- cout << "status_wmemcmp:" << status_wmemcmp << endl;
- //------------------------------------------------------------------------------------------------
- cout << endl;
- //------------------------------------------------------Сравнение через memcmp:----------------------
- const int len = strlen(my_char22);
- int status_memcmp = 0;
- clock1 = clock();
- for (size_t i = 0; i < num; i++)
- {
- const int pos = i % len;
- status_memcmp += memcmp(my_char11 + pos, my_char22 + pos, len - pos);
- }
- clock2 = clock();
- cout << "time_memcmp:" << clock2 - clock1 << endl;
- cout << "status_memcmp:" << status_memcmp << endl;
- //------------------------------------------------------------------------------------------------------
- cout << endl;
- //------------------------------------------------------Сравнение через GLIBC wmemcmp:-----------------------
- int status_glibc_wmemcmp = 0;
- clock1 = clock();
- for (size_t i = 0; i < num; i++)
- {
- const int pos = i % wlen;
- status_glibc_wmemcmp += glibc_wmemcmp(my_wchar11 + pos, my_wchar22 + pos, wlen - pos);
- }
- clock2 = clock();
- cout << "time_glibc_wmemcmp:" << clock2 - clock1 << endl;
- cout << "status_glibc_wmemcmp:" << status_glibc_wmemcmp << endl;
- //------------------------------------------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement