Advertisement
HimikoWerckmeister

Untitled

Mar 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <sys/syscall.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <stdlib.h>
  9.  
  10. void A(){
  11. }
  12.  
  13. double calculateAverage(double *arr, int len)
  14. {
  15. double average=0.00;
  16. //first sum up all entries
  17. for(int i=0;i<len;i++)
  18. {
  19. average+=arr[i];
  20. }
  21. return average;
  22. }
  23.  
  24. double timespec_to_ms(struct timespec *ts)
  25. {
  26. return ts->tv_sec*1000.0 + ts->tv_nsec/1000000.0;
  27. }
  28.  
  29. int main(int argc, char **argv)
  30. {
  31. struct timespec start_time, end_time;
  32.  
  33. if(argc <2)
  34. {
  35. printf("You entered less than two arguements\n");
  36. exit(-2);
  37. }
  38. if(argv[1] <0)
  39. {
  40. printf("you entered a negative argument\n");
  41. exit(-1);
  42. }
  43. //double *TestArray=malloc(sizeof(double)*atoi(argv[1]));
  44. //double *TestArray2=malloc(sizeof(double)*atoi(argv[1]));
  45. //double *TestArray3=malloc(sizeof(double)*atoi(argv[1]));
  46. //double *TestArray4=malloc(sizeof(double)*atoi(argv[1]));
  47.  
  48.  
  49. // Do empty 1
  50. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
  51. for(int i=0;i<(atoi(argv[1]));i++)
  52. {
  53.  
  54. /* begin timing */
  55. /*for(int i=0;i<1000000;i++)
  56. {
  57. //getpid();
  58. syscall(SYS_getpid);
  59. }*/
  60. /*int errorCount=0;
  61. for(int i=0;i<1000;i++)
  62. {
  63. int pid = fork();
  64. if (pid == 0) {
  65. return 0;
  66. }
  67. if (pid < 0) {
  68. printf("ERROR: Fork failed.\n");
  69. errorCount++;
  70. }
  71. wait(0);
  72.  
  73. }*/
  74. A();
  75.  
  76. /* end timing */
  77.  
  78. //TestArray[i]=(timespec_to_ms(&end_time)-timespec_to_ms(&start_time));
  79. }
  80. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
  81. printf("%f ms\n", timespec_to_ms(&end_time)
  82. - timespec_to_ms(&start_time));
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement