Advertisement
Guest User

Untitled

a guest
Apr 4th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN 1
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3.  
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <Windows.h>
  10.  
  11. #define LIMIT 999999U
  12.  
  13. static void test1(void)
  14. {
  15.     FILE *file = fopen("C:\\Temp\\test1.out", "wb");
  16.     if (!file)
  17.     {
  18.         abort();
  19.     }
  20.  
  21.     for (size_t i = 0; i < LIMIT; ++i)
  22.     {
  23.         uint8_t buffer = i % 256U;
  24.         if (fwrite(&buffer, sizeof(uint8_t), 1U, file) != 1U)
  25.         {
  26.             abort();
  27.         }
  28.     }
  29.  
  30.     fclose(file);
  31. }
  32.  
  33. static void test2(void)
  34. {
  35.     HANDLE h = CreateFileA("C:\\Temp\\test2.out", GENERIC_WRITE, 0U, NULL, CREATE_ALWAYS, 0U, NULL);
  36.     if (h == INVALID_HANDLE_VALUE)
  37.     {
  38.         abort();
  39.     }
  40.  
  41.     for (size_t i = 0; i < LIMIT; ++i)
  42.     {
  43.         uint8_t buffer = i % 256U;
  44.         DWORD written;
  45.         if (!WriteFile(h, &buffer, 1U, &written, NULL))
  46.         {
  47.             abort();
  48.         }
  49.         if (written != 1U)
  50.         {
  51.             abort();
  52.         }
  53.     }
  54.  
  55.     CloseHandle(h);
  56. }
  57.  
  58. int main()
  59. {
  60.     for (int i = 0; i < 3; ++i)
  61.     {
  62.         const clock_t a = clock();
  63.         test1();
  64.         const clock_t b = clock();
  65.         test2();
  66.         const clock_t c = clock();
  67.  
  68.         printf("stdio: %lu\n", b - a);
  69.         printf("win32: %lu (x%.f)\n", c - b, (c - b) / ((double)(b - a)));
  70.         puts("");
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement