aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rwxr-xr-xcsv-parser.py18
-rwxr-xr-xcsv-parser.rb10
-rw-r--r--csv-perf.c115
4 files changed, 40 insertions, 106 deletions
diff --git a/.gitignore b/.gitignore
index 52ff689..6b6b758 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,6 +15,3 @@ Vegleich.ods
15*.md.backup 15*.md.backup
16*.html 16*.html
17 17
18csv-parser.py
19csv-parser.rb
20
diff --git a/csv-parser.py b/csv-parser.py
new file mode 100755
index 0000000..9029321
--- /dev/null
+++ b/csv-parser.py
@@ -0,0 +1,18 @@
1#! /usr/local/bin/python3.7
2
3import csv, time, sys
4
5with 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/csv-parser.rb b/csv-parser.rb
new file mode 100755
index 0000000..aaac877
--- /dev/null
+++ b/csv-parser.rb
@@ -0,0 +1,10 @@
1#! /usr/local/bin/ruby
2
3require 'csv'
4
5startTime = Time.now
6CSV.read(ARGV[0])
7endTime = Time.now
8
9print(endTime-startTime)
10puts "\n"
diff --git a/csv-perf.c b/csv-perf.c
index ddb4808..51bf9ff 100644
--- a/csv-perf.c
+++ b/csv-perf.c
@@ -1,17 +1,17 @@
1#include <stdio.h> 1#include <assert.h>
2#include <stdlib.h>
3#include <setjmp.h> 2#include <setjmp.h>
4#include <signal.h> 3#include <signal.h>
4#include <stdio.h>
5#include <stdlib.h>
5#include <time.h> 6#include <time.h>
6#include <assert.h>
7 7
8#include "csv.h" 8#include "csv.h"
9 9
10static int 10static int
11test(FILE *f) 11test(FILE *f)
12{ 12{
13 int line = 0; 13 int line = 0;
14 csv_t csv = { 0 }; 14 csv_t csv = { 0 };
15 15
16 while ( csv_read(&csv, f) != EOF ) { 16 while ( csv_read(&csv, f) != EOF ) {
17 ++line; 17 ++line;
@@ -20,103 +20,15 @@ test(FILE *f)
20 return line; 20 return line;
21} 21}
22 22
23static int
24test_exception(FILE *f)
25{
26 int line = 0;
27 csv_t csv[1];
28
29 csv_init(csv);
30 for ( int n; (n = csv_read(csv, f)) != EOF; ) {
31 if (++line == 500000) csv_field(csv, -1);
32 }
33
34 return line;
35}
36
37static void
38my_signal_handler(int sig)
39{
40 if ( sig == SIGABRT ) {
41 fprintf(stderr, "my_signal_handler!\n");
42 exit(1);
43 }
44}
45
46static int
47test_exception_with_signal(FILE *f)
48{
49 void (*old_handler)(int) = signal(SIGABRT, my_signal_handler);
50
51 int line = 0;
52 csv_t csv[1];
53
54 csv_init(csv);
55 for ( int n; (n = csv_read(csv, f)) != EOF; ) {
56 if (++line == 500000) csv_field(csv, -1);
57 }
58
59 signal(SIGABRT, old_handler);
60
61 return line;
62}
63
64static void
65my_error_handler(csv_err_t csv_err, void *env)
66{
67 assert(env != NULL);
68
69 fprintf(stderr, "my_error_handler: %s\n", csv_err_str(csv_err));
70 longjmp(*((jmp_buf *) env), 1);
71}
72
73static int
74test_exception_longjmp(FILE *f)
75{
76 /* start with default options */
77 csv_options_t csv_options = csv_default_options;
78
79 /* setup error handler */
80 jmp_buf env;
81 csv_options.cb_error = my_error_handler;
82 csv_options.cb_error_arg = &env;
83
84 /* setup csv-Object with custom options */
85 csv_t csv[1];
86 csv_init_opt(csv, &csv_options);
87
88 if ( setjmp(env) == 0 ) {
89 int line = 0;
90
91 for ( int n; (n = csv_read(csv, f)) != EOF; ) {
92 // process data...
93 if (++line == 500000) csv_field(csv, -1);
94 }
95 return line;
96 }
97 else {
98 csv_cleanup(csv);
99 return -1;
100 }
101}
102
103int 23int
104main(void) 24main(int argc, char *argv[])
105{ 25{
106#if 0 26 if ( argc != 2 ) {
107 const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL }; 27 fprintf(stderr, "usage: %s testfile\n", argv[0]);
108 28 return EXIT_FAILURE;
109 csv_write(stdout, -1, names, NULL); 29 }
110 csv_write(stdout, 0, names, NULL);
111 csv_write(stdout, 1, names, NULL);
112 csv_write(stdout, 2, names, NULL);
113 csv_write(stdout, 3, names, NULL);
114 csv_write(stdout, 4, names, NULL);
115 csv_write(stdout, 5, names, NULL);
116 return 0;
117#endif
118 30
119 FILE *in = fopen("500000 Records.csv", "r"); 31 FILE *in = fopen(argv[1], "r");
120 if ( in == NULL ) { 32 if ( in == NULL ) {
121 fprintf(stderr, "failed to open testfile...\n"); 33 fprintf(stderr, "failed to open testfile...\n");
122 return EXIT_FAILURE; 34 return EXIT_FAILURE;
@@ -125,15 +37,12 @@ main(void)
125 clock_t start = clock(); 37 clock_t start = clock();
126 38
127 int lines = test(in); 39 int lines = test(in);
128 (void) test_exception;
129 (void) test_exception_with_signal;
130 (void) test_exception_longjmp;
131 40
132 clock_t end = clock(); 41 clock_t end = clock();
133 42
134 fclose(in); 43 fclose(in);
135 44
136 printf("%d lines processed, duration: %.3lf sec\n", lines, ((double)(end - start)) / CLOCKS_PER_SEC); 45 printf("%d lines processed, duration: %.3lf sec\n", lines, ((double) (end - start)) / CLOCKS_PER_SEC);
137 46
138 return EXIT_SUCCESS; 47 return EXIT_SUCCESS;
139} 48}