Skip to content

Instantly share code, notes, and snippets.

@gunavaran
Created June 20, 2020 06:15
Show Gist options
  • Save gunavaran/7f2b8f97adc9052119bbf446aacf0549 to your computer and use it in GitHub Desktop.
Save gunavaran/7f2b8f97adc9052119bbf446aacf0549 to your computer and use it in GitHub Desktop.
int testfgets(char *fastqFile) {
clock_t start, end;
double cpu_time_used;
char *line = malloc(sizeof(char) * MAXLINELEN);
start = clock();
FILE *ptr;
if ((ptr = fopen(fastqFile, "r")) == NULL) {
printf("no such file as %s \n", fastqFile);
return 0;
}
char *aline; /* this variable is not used, it suppresses a compiler warning */
while ((aline = fgets(line, MAXLINELEN, ptr)) != NULL) {
//do something
}
fclose(ptr);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("time taken for fgets: %f \n", cpu_time_used);
}
int testGetLine(char *fastqFile) {
clock_t start, end;
double cpu_time_used;
char *line = malloc(sizeof(char) * MAXLINELEN);
start = clock();
FILE * ptr;
size_t len = MAXLINELEN;
ssize_t read;
ptr = fopen(fastqFile, "r");
if (ptr == NULL){
printf("no such file as %s \n", fastqFile);
return 0;
}
while ((read = getline(&line, &len, ptr)) != -1) {
//do something
}
fclose(ptr);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("time taken for getline: %f \n", cpu_time_used);
}
int testfscanf(char *fastqFile) {
clock_t start, end;
double cpu_time_used;
char *line = malloc(sizeof(char) * MAXLINELEN);
start = clock();
FILE *ptr;
if ((ptr = fopen(fastqFile, "r")) == NULL) {
printf("no such file as %s \n", fastqFile);
return 0;
}
while (fscanf(ptr, "%[^\n]\n", line) == 1) {
//do something
}
fclose(ptr);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("time taken for fscanf: %f \n", cpu_time_used);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment