-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.c
68 lines (61 loc) · 1.74 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "create_fractal.h"
#ifdef CLOCK_PROCESS_CPUTIME_ID
// call this function to start a nanosecond-resolution timer
struct timespec timer_start(){
struct timespec start_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
return start_time;
}
// call this function to end a timer, returning nanoseconds elapsed as a long
long timer_end(struct timespec start_time){
struct timespec end_time;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
long diffInNanos = end_time.tv_nsec - start_time.tv_nsec;
return diffInNanos;
}
#else
#include <sys\timeb.h>
#endif
int main(int argc, const char *argv[], const char * env[])
{
int width = 1500;
int height = 1000;
int iters = 20;
FILE * fid = NULL;
Img img;
size_t written;
#ifdef CLOCK_PROCESS_CPUTIME_ID
struct timespec vartime;
#else
struct timeb start, stop;
#endif
long time_elapsed_nanos;
img.width = width;
img.height = height;
img.data = (unsigned char*)malloc(width * height * sizeof(unsigned char));
if (NULL == img.data)
return -1;
#ifdef CLOCK_PROCESS_CPUTIME_ID
vartime = timer_start();
#else
ftime(&start);
#endif
create_fractal(img, iters);
#ifdef CLOCK_PROCESS_CPUTIME_ID
time_elapsed_nanos = timer_end(vartime);
#else
ftime(&stop);
time_elapsed_nanos = 1000000L * ((int) (1000.0 * (stop.time - start.time)
+ (stop.millitm - start.millitm)));
#endif
fprintf(stdout, "create_fractal required %ld millisecs\n", time_elapsed_nanos / 1000000);
fid = fopen("c.raw", "wb");
if (NULL == fid)
return -2;
written = fwrite(img.data, sizeof(unsigned char), width * height, fid);
fclose(fid);
return 0;
}