Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE 1000 * 1000 * 400
- double timing(clock_t start, clock_t finish) {
- return ((double) finish - start) / CLOCKS_PER_SEC;
- }
- int main() {
- int *a = calloc(SIZE, sizeof(int));
- /* walk over everything once to normalize for cache effects */
- for (int i = 0; i < SIZE; i++) { int z = a[i]; }
- /* The unchecked version */
- clock_t start_unchecked = clock();
- for (int i = 0; i < SIZE; i++) {
- int z = a[i];
- }
- clock_t finish_unchecked = clock();
- /* walk over everything once to normalize for cache effects */
- for (int i = 0; i < SIZE; i++) { int z = a[i]; }
- /* The unchecked version */
- clock_t start_checked = clock();
- for (int i = 0; i < SIZE; i++) {
- if (i < SIZE) {
- int z = a[i];
- }
- }
- clock_t finish_checked = clock();
- printf("Checked: %f, Unchecked: %f",
- timing(start_unchecked, finish_unchecked),
- timing(start_checked, finish_checked));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement