Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <sys/time.h>
  6. // Simple routine to return absolute time (in seconds).
  7. double Time_GetSeconds() {
  8. struct timeval t;
  9. int rc = gettimeofday(&t, NULL);
  10. assert(rc == 0);
  11. return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6);
  12. }
  13. // Program that allocates an array of ints of certain size,
  14. // and then proceeeds to update each int in a loop, forever.
  15. int main(int argc, char *argv[]) {
  16. if (argc != 2) {
  17. fprintf(stderr, "usage: spin <memory (MB)>\n");
  18. exit(1);
  19. }
  20. long long int size = (long long int) atoi(argv[1]);
  21. long long int size_in_bytes = size * 1024 * 1024;
  22. printf("allocating %lld bytes (%.2f MB)\n",
  23. size_in_bytes, size_in_bytes / (1024 * 1024.0));
  24. // the big memory allocation happens here
  25. int *x = malloc(size_in_bytes);
  26. if (x == NULL) {
  27. fprintf(stderr, "memory allocation failed\n");
  28. exit(1);
  29. }
  30. long long int num_ints = size_in_bytes / sizeof(int);
  31. printf(" number of integers in array: %lld\n", num_ints);
  32. // now the main loop: each time through, touch each integer
  33. // (and increment its value by 1).
  34. int i = 0;
  35. double time_since_last_print = 2.0;
  36. double t = Time_GetSeconds();
  37. int loop_count = 0;
  38. while (1) {
  39. x[i++] += 1; // main work of loop done here.
  40. // if we've gone through the whole loop, reset a bunch of stuff
  41. // and then (perhaps) print out some statistics.
  42. if (i == num_ints) {
  43. double delta_time = Time_GetSeconds() - t;
  44. time_since_last_print += delta_time;
  45. if (time_since_last_print >= 0.2) { // only print every .2 seconds
  46. printf("loop %d in %.2f ms (bandwidth: %.2f MB/s)\n",
  47. loop_count, 1000 * delta_time,
  48. size_in_bytes / (1024.0*1024.0*delta_time));
  49. time_since_last_print = 0;
  50. }
  51. i = 0;
  52. t = Time_GetSeconds();
  53. loop_count++;
  54. }
  55. }
  56. return 0;
  57. }