Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program to demonstrate time taken by function fun() */
- #include <stdio.h>
- #include <time.h>
- float a[1000000]; //the type is changed (float, char, double, int) and size of array
- void fun()
- {
- srand(time(NULL));
- for (int i=1; i<=1000000; i++)
- {
- a[i-1] = rand();
- }
- }
- int main()
- {
- clock_t t;
- t = clock();
- fun();
- t = clock() - t;
- double writing_time = ((double)t)/CLOCKS_PER_SEC; // in seconds
- clock_t read;
- read=clock();
- for (int j=1; j<=1000000; j++)
- {
- int x;
- x = a[j];
- }
- read=clock() - read;
- double reading_time = ((double)read)/CLOCKS_PER_SEC;
- printf("fun() took %f seconds to write \n", writing_time);
- printf("fun() took %f seconds to read \n", reading_time);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement