Advertisement
Guest User

Untitled

a guest
May 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  Assembler
  4. //
  5. //  Created by Serge Nanaev on 28.05.17.
  6. //  Copyright © 2017 Serge Nanaev. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <ctime>
  11. #include <sys/time.h>
  12.  
  13. float timedifference_msec(struct timeval t0, struct timeval t1) {
  14.     return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
  15. }
  16.  
  17. int main(int argc, const char * argv[]) {
  18.  
  19.     int checkNumber = 45;
  20.     int arraySize = 250;
  21.     int array[arraySize][arraySize];
  22.  
  23.     for (int i = 0; i < arraySize; i++) {
  24.         for (int j = 0; j < arraySize; j++) {
  25.             array[i][j] = rand() % 100;
  26.         }
  27.     }
  28.  
  29.     struct timeval t0;
  30.     struct timeval t1;
  31.     float elapsed;
  32.  
  33.     gettimeofday(&t0, 0);
  34.  
  35.     for (int n = 0; n < 1000; n++) {
  36.         for (int i = 0; i < arraySize; i++) {
  37.             int sum = 0;
  38.             for (int j = 0; j < arraySize; j++) {
  39.                 sum += array[i][j];
  40.             }
  41.             int avg = sum/arraySize;
  42.             //std::cout << avg << '\n';
  43.             if (avg < checkNumber) {
  44.                 //std::cout << "Find it! \n";
  45.             }
  46.         }
  47.     }
  48.  
  49.     gettimeofday(&t1, 0);
  50.  
  51.     elapsed = timedifference_msec(t0, t1);
  52.  
  53.     printf("Code executed in %f milliseconds.\n", elapsed);
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement