diff options
Diffstat (limited to 'contrib')
| -rwxr-xr-x | contrib/csv-parser.py | 18 | ||||
| -rwxr-xr-x | contrib/csv-parser.rb | 10 | ||||
| -rw-r--r-- | contrib/csv-perf.c | 46 | ||||
| -rw-r--r-- | contrib/csv-tutorial.c | 106 | ||||
| -rw-r--r-- | contrib/seq-read.c | 20 |
5 files changed, 200 insertions, 0 deletions
diff --git a/contrib/csv-parser.py b/contrib/csv-parser.py new file mode 100755 index 0000000..9029321 --- /dev/null +++ b/contrib/csv-parser.py | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #! /usr/local/bin/python3.7 | ||
| 2 | |||
| 3 | import csv, time, sys | ||
| 4 | |||
| 5 | with open(sys.argv[1]) as csv_file: | ||
| 6 | start = time.time() | ||
| 7 | csv_reader = csv.reader(csv_file, delimiter=',') | ||
| 8 | line_count = 0 | ||
| 9 | for row in csv_reader: | ||
| 10 | if line_count == 0: | ||
| 11 | line_count += 1 | ||
| 12 | else: | ||
| 13 | line_count += 1 | ||
| 14 | |||
| 15 | end = time.time() | ||
| 16 | print(f'Processed {line_count} lines.') | ||
| 17 | print(end - start) | ||
| 18 | |||
diff --git a/contrib/csv-parser.rb b/contrib/csv-parser.rb new file mode 100755 index 0000000..aaac877 --- /dev/null +++ b/contrib/csv-parser.rb | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #! /usr/local/bin/ruby | ||
| 2 | |||
| 3 | require 'csv' | ||
| 4 | |||
| 5 | startTime = Time.now | ||
| 6 | CSV.read(ARGV[0]) | ||
| 7 | endTime = Time.now | ||
| 8 | |||
| 9 | print(endTime-startTime) | ||
| 10 | puts "\n" | ||
diff --git a/contrib/csv-perf.c b/contrib/csv-perf.c new file mode 100644 index 0000000..1c0f7a3 --- /dev/null +++ b/contrib/csv-perf.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #include <assert.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <time.h> | ||
| 5 | |||
| 6 | #include "csv.h" | ||
| 7 | |||
| 8 | static int | ||
| 9 | test(FILE *f) | ||
| 10 | { | ||
| 11 | int line = 0; | ||
| 12 | csv_t csv = { 0 }; | ||
| 13 | |||
| 14 | while ( csv_read(&csv, f) ) { | ||
| 15 | ++line; | ||
| 16 | } | ||
| 17 | |||
| 18 | return line; | ||
| 19 | } | ||
| 20 | |||
| 21 | int | ||
| 22 | main(int argc, char *argv[]) | ||
| 23 | { | ||
| 24 | if ( argc != 2 ) { | ||
| 25 | fprintf(stderr, "usage: %s testfile\n", argv[0]); | ||
| 26 | return EXIT_FAILURE; | ||
| 27 | } | ||
| 28 | |||
| 29 | FILE *in = fopen(argv[1], "r"); | ||
| 30 | if ( in == NULL ) { | ||
| 31 | fprintf(stderr, "failed to open testfile...\n"); | ||
| 32 | return EXIT_FAILURE; | ||
| 33 | } | ||
| 34 | |||
| 35 | clock_t start = clock(); | ||
| 36 | |||
| 37 | int lines = test(in); | ||
| 38 | |||
| 39 | clock_t end = clock(); | ||
| 40 | |||
| 41 | fclose(in); | ||
| 42 | |||
| 43 | printf("%d lines processed, duration: %.3lf sec\n", lines, ((double) (end - start)) / CLOCKS_PER_SEC); | ||
| 44 | |||
| 45 | return EXIT_SUCCESS; | ||
| 46 | } | ||
diff --git a/contrib/csv-tutorial.c b/contrib/csv-tutorial.c new file mode 100644 index 0000000..9bdb7e6 --- /dev/null +++ b/contrib/csv-tutorial.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #include <setjmp.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | |||
| 6 | #include "csv.h" | ||
| 7 | |||
| 8 | bool | ||
| 9 | processFile(const char *filename) | ||
| 10 | { | ||
| 11 | FILE *in = fopen(filename, "r"); | ||
| 12 | |||
| 13 | if ( in == NULL ) { | ||
| 14 | return false; | ||
| 15 | } | ||
| 16 | |||
| 17 | csv_t csv; | ||
| 18 | csv_init(&csv); | ||
| 19 | |||
| 20 | while ( csv_read(&csv, in) ) { | ||
| 21 | const size_t nfields = csv_nfields(&csv); | ||
| 22 | // ... | ||
| 23 | } | ||
| 24 | csv_cleanup(&csv); | ||
| 25 | |||
| 26 | (void) fclose(in); | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | |||
| 30 | bool | ||
| 31 | processFile_options(const char *filename) | ||
| 32 | { | ||
| 33 | FILE *in = fopen(filename, "r"); | ||
| 34 | |||
| 35 | if ( in == NULL ) { | ||
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | csv_options_t csv_options = csv_default_options; | ||
| 40 | csv_options.field_delimiter = '"'; | ||
| 41 | csv_options.field_separator = ','; | ||
| 42 | |||
| 43 | csv_t csv; | ||
| 44 | csv_init_opt(&csv, &csv_options); | ||
| 45 | |||
| 46 | while ( csv_read(&csv, in) ) { | ||
| 47 | const size_t nfields = csv_nfields(&csv); | ||
| 48 | // ... | ||
| 49 | } | ||
| 50 | csv_cleanup(&csv); | ||
| 51 | |||
| 52 | (void) fclose(in); | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | |||
| 56 | static void | ||
| 57 | csvErrorHandler(csv_err_t err, void *cb_arg) | ||
| 58 | { | ||
| 59 | longjmp(*((jmp_buf *) cb_arg), err); | ||
| 60 | } | ||
| 61 | |||
| 62 | bool | ||
| 63 | processFile_error(const char *filename) | ||
| 64 | { | ||
| 65 | FILE *in = fopen(filename, "r"); | ||
| 66 | |||
| 67 | if ( in == NULL ) { | ||
| 68 | return false; | ||
| 69 | } | ||
| 70 | |||
| 71 | csv_options_t csv_options = csv_default_options; | ||
| 72 | csv_options.field_delimiter = '"'; | ||
| 73 | csv_options.field_separator = ','; | ||
| 74 | |||
| 75 | jmp_buf env; | ||
| 76 | csv_options.cb_error = csvErrorHandler; | ||
| 77 | csv_options.cb_error_arg = &env; | ||
| 78 | |||
| 79 | csv_t csv; | ||
| 80 | csv_init_opt(&csv, &csv_options); | ||
| 81 | |||
| 82 | switch ( setjmp(env) ) { | ||
| 83 | case CSV_ERR_OK: | ||
| 84 | while ( csv_read(&csv, in) ) { | ||
| 85 | const size_t nfields = csv_nfields(&csv); | ||
| 86 | // ... | ||
| 87 | } | ||
| 88 | break; | ||
| 89 | |||
| 90 | case CSV_ERR_OUT_OF_MEMORY: | ||
| 91 | case CSV_ERR_OUT_OF_RANGE: | ||
| 92 | case CSV_ERR_IO_READ: | ||
| 93 | case CSV_ERR_IO_WRITE: | ||
| 94 | csv_cleanup(&csv); | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | |||
| 98 | (void) fclose(in); | ||
| 99 | return true; | ||
| 100 | } | ||
| 101 | |||
| 102 | int | ||
| 103 | main(void) | ||
| 104 | { | ||
| 105 | return EXIT_SUCCESS; | ||
| 106 | } | ||
diff --git a/contrib/seq-read.c b/contrib/seq-read.c new file mode 100644 index 0000000..90d201f --- /dev/null +++ b/contrib/seq-read.c | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <time.h> | ||
| 4 | |||
| 5 | int | ||
| 6 | main(void) | ||
| 7 | { | ||
| 8 | clock_t start = clock(); | ||
| 9 | size_t sum = 0; | ||
| 10 | |||
| 11 | for ( int ch; (ch = getc(stdin)) != EOF; ) { | ||
| 12 | sum += (unsigned char) ch; | ||
| 13 | } | ||
| 14 | |||
| 15 | clock_t end = clock(); | ||
| 16 | |||
| 17 | printf("Result: %zu, Duration: %.3lf sec\n", sum, ((double)(end - start)) / CLOCKS_PER_SEC); | ||
| 18 | |||
| 19 | return EXIT_SUCCESS; | ||
| 20 | } | ||
