...

Text file src/github.com/yuin/goldmark/_benchmark/cmark/cmark_benchmark.c

Documentation: github.com/yuin/goldmark/_benchmark/cmark

     1#include <stdio.h>
     2#include <stdlib.h>
     3#ifdef WIN32
     4#  include <windows.h>
     5#else
     6#  include <sys/time.h>
     7#  include <sys/resource.h>
     8#endif
     9#include "cmark.h"
    10
    11
    12#ifdef WIN32
    13
    14double get_time()
    15{
    16    LARGE_INTEGER t, f;
    17    QueryPerformanceCounter(&t);
    18    QueryPerformanceFrequency(&f);
    19    return (double)t.QuadPart/(double)f.QuadPart;
    20}
    21
    22#else
    23
    24
    25double get_time()
    26{
    27    struct timeval t;
    28    struct timezone tzp;
    29    gettimeofday(&t, &tzp);
    30    return t.tv_sec + t.tv_usec*1e-6;
    31}
    32
    33#endif
    34
    35int main(int argc, char **argv) {
    36    char *markdown_file;
    37    FILE *fp;
    38    size_t size;
    39    char *buf;
    40    char *html;
    41    double start, sum;
    42    int i, n;
    43
    44    n = argc > 1 ? atoi(argv[1]) : 50;
    45    markdown_file = argc > 2 ? argv[2] : "_data.md";
    46
    47    fp = fopen(markdown_file,"r");
    48    if(fp == NULL){
    49      fprintf(stderr, "can not open %s", markdown_file);
    50      exit(1);
    51    }
    52
    53    if(fseek(fp, 0, SEEK_END) != 0) {
    54      fprintf(stderr, "can not seek %s", markdown_file);
    55      exit(1);
    56    }
    57    if((size = ftell(fp)) < 0) {
    58      fprintf(stderr, "can not get size of %s", markdown_file);
    59      exit(1);
    60    }
    61    if(fseek(fp, 0, SEEK_SET) != 0) {
    62      fprintf(stderr, "can not seek %s", markdown_file);
    63      exit(1);
    64    }
    65    buf = malloc(sizeof(char) * size);
    66    if(buf == NULL) {
    67      fprintf(stderr, "can not allocate memory for %s", markdown_file);
    68      exit(1);
    69    }
    70
    71    if(fread(buf, 1, size, fp) < size) {
    72      fprintf(stderr, "failed to read for %s", markdown_file);
    73      exit(1);
    74    }
    75
    76    fclose(fp);
    77
    78    for(i = 0; i < n; i++) {
    79      start = get_time();
    80      html = cmark_markdown_to_html(buf, size, CMARK_OPT_UNSAFE);
    81      free(html);
    82      sum += get_time() - start;
    83    }
    84    printf("----------- cmark -----------\n");
    85    printf("file: %s\n", markdown_file);
    86    printf("iteration: %d\n", n);
    87    printf("average: %.10f sec\n", sum / (double)n);
    88
    89    free(buf);
    90    return 0;
    91}

View as plain text