Advertisement
pakuula

memcmp vs wmemcmp

Dec 1st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int
  8. glibc_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
  9. {
  10.   wchar_t c1;
  11.   wchar_t c2;
  12.  
  13.   while (n >= 4)
  14.     {
  15.       c1 = s1[0];
  16.       c2 = s2[0];
  17.       if (c1 - c2 != 0)
  18.     return c1 > c2 ? 1 : -1;
  19.       c1 = s1[1];
  20.       c2 = s2[1];
  21.       if (c1 - c2 != 0)
  22.     return c1 > c2 ? 1 : -1;
  23.       c1 = s1[2];
  24.       c2 = s2[2];
  25.       if (c1 - c2 != 0)
  26.     return c1 > c2 ? 1 : -1;
  27.       c1 = s1[3];
  28.       c2 = s2[3];
  29.       if (c1 - c2 != 0)
  30.     return c1 > c2 ? 1 : -1;
  31.       s1 += 4;
  32.       s2 += 4;
  33.       n -= 4;
  34.     }
  35.  
  36.   if (n > 0)
  37.     {
  38.       c1 = s1[0];
  39.       c2 = s2[0];
  40.       if (c1 - c2 != 0)
  41.     return c1 > c2 ? 1 : -1;
  42.       ++s1;
  43.       ++s2;
  44.       --n;
  45.     }
  46.   if (n > 0)
  47.     {
  48.       c1 = s1[0];
  49.       c2 = s2[0];
  50.       if (c1 - c2 != 0)
  51.     return c1 > c2 ? 1 : -1;
  52.       ++s1;
  53.       ++s2;
  54.       --n;
  55.     }
  56.   if (n > 0)
  57.     {
  58.       c1 = s1[0];
  59.       c2 = s2[0];
  60.       if (c1 - c2 != 0)
  61.     return c1 > c2 ? 1 : -1;
  62.     }
  63.  
  64.   return 0;
  65. }
  66.  
  67. int main(int argc, char ** argv) {
  68.     const wchar_t my_wchar11[] = L"rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
  69.     const wchar_t my_wchar22[] = L"rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
  70.  
  71.     const char my_char11[] = "rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
  72.     const char my_char22[] = "rstuw12345612345ab89abcefghijklmnopqrstuw123457q";
  73.  
  74. //----------------------------------------------------------------------------------------
  75.  
  76.     const size_t num = 99999999;
  77.     int clock1;
  78.     int clock2;
  79.  
  80.     //------------------------------------------------------Сравнение через wmemcmp:-----------------------
  81.  
  82.     int status_wmemcmp = 0;
  83.     const int wlen = wcslen(my_wchar22);
  84.     clock1 = clock();
  85.     for (size_t i = 0; i < num; i++)
  86.     {
  87.         const int pos = i % wlen;
  88.         status_wmemcmp += wmemcmp(my_wchar11 + pos, my_wchar22 + pos, wlen - pos);
  89.     }
  90.     clock2 = clock();
  91.  
  92.     cout << "time_wmemcmp:" << clock2 - clock1 << endl;
  93.     cout << "status_wmemcmp:" << status_wmemcmp << endl;
  94.  
  95.     //------------------------------------------------------------------------------------------------
  96.  
  97.     cout << endl;
  98.  
  99.     //------------------------------------------------------Сравнение через memcmp:----------------------
  100.  
  101.     const int len = strlen(my_char22);
  102.     int status_memcmp = 0;
  103.     clock1 = clock();
  104.     for (size_t i = 0; i < num; i++)
  105.     {
  106.         const int pos = i % len;
  107.         status_memcmp += memcmp(my_char11 + pos, my_char22 + pos, len - pos);
  108.     }
  109.     clock2 = clock();
  110.  
  111.     cout << "time_memcmp:" << clock2 - clock1 << endl;
  112.     cout << "status_memcmp:" << status_memcmp << endl;
  113.     //------------------------------------------------------------------------------------------------------
  114.  
  115.     cout << endl;
  116.  
  117.     //------------------------------------------------------Сравнение через GLIBC wmemcmp:-----------------------
  118.  
  119.     int status_glibc_wmemcmp = 0;
  120.     clock1 = clock();
  121.     for (size_t i = 0; i < num; i++)
  122.     {
  123.         const int pos = i % wlen;
  124.         status_glibc_wmemcmp += glibc_wmemcmp(my_wchar11 + pos, my_wchar22 + pos, wlen - pos);
  125.     }
  126.     clock2 = clock();
  127.  
  128.     cout << "time_glibc_wmemcmp:" << clock2 - clock1 << endl;
  129.     cout << "status_glibc_wmemcmp:" << status_glibc_wmemcmp << endl;
  130.  
  131.     //------------------------------------------------------------------------------------------------
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement