From 4a77a81d349d81bf9ed479bb3b8ddedb1c22aa82 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 8 Jul 2020 18:35:26 +0200 Subject: Vergleichsprogramme in Ruby und Python hinzugefügt. csv-perf.c von unnötigem Code befreit. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 -- csv-parser.py | 18 +++++++++ csv-parser.rb | 10 +++++ csv-perf.c | 115 ++++++---------------------------------------------------- 4 files changed, 40 insertions(+), 106 deletions(-) create mode 100755 csv-parser.py create mode 100755 csv-parser.rb diff --git a/.gitignore b/.gitignore index 52ff689..6b6b758 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,3 @@ Vegleich.ods *.md.backup *.html -csv-parser.py -csv-parser.rb - 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 @@ +#! /usr/local/bin/python3.7 + +import csv, time, sys + +with open(sys.argv[1]) as csv_file: + start = time.time() + csv_reader = csv.reader(csv_file, delimiter=',') + line_count = 0 + for row in csv_reader: + if line_count == 0: + line_count += 1 + else: + line_count += 1 + + end = time.time() + print(f'Processed {line_count} lines.') + print(end - start) + 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 @@ +#! /usr/local/bin/ruby + +require 'csv' + +startTime = Time.now +CSV.read(ARGV[0]) +endTime = Time.now + +print(endTime-startTime) +puts "\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 @@ -#include -#include +#include #include #include +#include +#include #include -#include #include "csv.h" static int test(FILE *f) { - int line = 0; - csv_t csv = { 0 }; + int line = 0; + csv_t csv = { 0 }; while ( csv_read(&csv, f) != EOF ) { ++line; @@ -20,103 +20,15 @@ test(FILE *f) return line; } -static int -test_exception(FILE *f) -{ - int line = 0; - csv_t csv[1]; - - csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != EOF; ) { - if (++line == 500000) csv_field(csv, -1); - } - - return line; -} - -static void -my_signal_handler(int sig) -{ - if ( sig == SIGABRT ) { - fprintf(stderr, "my_signal_handler!\n"); - exit(1); - } -} - -static int -test_exception_with_signal(FILE *f) -{ - void (*old_handler)(int) = signal(SIGABRT, my_signal_handler); - - int line = 0; - csv_t csv[1]; - - csv_init(csv); - for ( int n; (n = csv_read(csv, f)) != EOF; ) { - if (++line == 500000) csv_field(csv, -1); - } - - signal(SIGABRT, old_handler); - - return line; -} - -static void -my_error_handler(csv_err_t csv_err, void *env) -{ - assert(env != NULL); - - fprintf(stderr, "my_error_handler: %s\n", csv_err_str(csv_err)); - longjmp(*((jmp_buf *) env), 1); -} - -static int -test_exception_longjmp(FILE *f) -{ - /* start with default options */ - csv_options_t csv_options = csv_default_options; - - /* setup error handler */ - jmp_buf env; - csv_options.cb_error = my_error_handler; - csv_options.cb_error_arg = &env; - - /* setup csv-Object with custom options */ - csv_t csv[1]; - csv_init_opt(csv, &csv_options); - - if ( setjmp(env) == 0 ) { - int line = 0; - - for ( int n; (n = csv_read(csv, f)) != EOF; ) { - // process data... - if (++line == 500000) csv_field(csv, -1); - } - return line; - } - else { - csv_cleanup(csv); - return -1; - } -} - int -main(void) +main(int argc, char *argv[]) { -#if 0 - const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL }; - - csv_write(stdout, -1, names, NULL); - csv_write(stdout, 0, names, NULL); - csv_write(stdout, 1, names, NULL); - csv_write(stdout, 2, names, NULL); - csv_write(stdout, 3, names, NULL); - csv_write(stdout, 4, names, NULL); - csv_write(stdout, 5, names, NULL); - return 0; -#endif + if ( argc != 2 ) { + fprintf(stderr, "usage: %s testfile\n", argv[0]); + return EXIT_FAILURE; + } - FILE *in = fopen("500000 Records.csv", "r"); + FILE *in = fopen(argv[1], "r"); if ( in == NULL ) { fprintf(stderr, "failed to open testfile...\n"); return EXIT_FAILURE; @@ -125,15 +37,12 @@ main(void) clock_t start = clock(); int lines = test(in); - (void) test_exception; - (void) test_exception_with_signal; - (void) test_exception_longjmp; clock_t end = clock(); fclose(in); - printf("%d lines processed, duration: %.3lf sec\n", lines, ((double)(end - start)) / CLOCKS_PER_SEC); + printf("%d lines processed, duration: %.3lf sec\n", lines, ((double) (end - start)) / CLOCKS_PER_SEC); return EXIT_SUCCESS; } -- cgit v1.3