Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- int a[1000];
- int b[1000];
- void setAandB()
- {
- int i = 0;
- for(i = 0; i < 1000; i++) {
- a[i] = 0;
- b[i] = i;
- }
- }
- void bar()
- {
- int i = 0;
- while (i < 1000){
- a[i] += b[i++];
- a[i] += b[i++];
- a[i] += b[i++];
- a[i] += b[i++];
- }
- }
- void foo() {
- int i;
- for (i = 0; i < 1000; i++) {
- a[i] = a[i] + b[i];
- }
- }
- int main()
- {
- // setAandB();
- clock_t tic = clock();
- bar(); //slower
- clock_t toc = clock();
- printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
- // setAandB();
- clock_t tic1 = clock();
- foo(); //faster
- clock_t toc1 = clock();
- printf("Elapsed: %f seconds\n", (double)(toc1 - tic1) / CLOCKS_PER_SEC);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement