From 5339b9050a1e2bae6b8013f72e1a4e43f940290c Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 31 Aug 2016 19:05:56 -0400 Subject: add java, refactor ruby --- .gitignore | 2 ++ JavaCSVTest.java | 29 +++++++++++++++++++++++++++++ README.txt | 3 +++ bench.rb | 4 ++++ opencsv-2.3.jar | Bin 0 -> 19526 bytes rb-test.rb | 12 +++++++++--- results.txt | 8 ++++---- 7 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 JavaCSVTest.java create mode 100644 opencsv-2.3.jar diff --git a/.gitignore b/.gitignore index 72699f7..a9ab7de 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /*.csv +/*.class +/cr-test diff --git a/JavaCSVTest.java b/JavaCSVTest.java new file mode 100644 index 0000000..91ee366 --- /dev/null +++ b/JavaCSVTest.java @@ -0,0 +1,29 @@ +import au.com.bytecode.opencsv.CSVReader; +import au.com.bytecode.opencsv.CSVWriter; +import java.io.FileReader; +import java.io.FileWriter; + +public class JavaCSVTest { + private static String[] concat(final String a[], final String b[]) { + final String r[] = new String[a.length + b.length]; + + System.arraycopy(a, 0, r, 0, a.length); + System.arraycopy(b, 0, r, a.length, b.length); + + return r; + } + + private static final String FOO[] = { "foo", "foo" }; + + public static void main(String args[]) throws Exception { + final String src_path = args[0], dst_path = args[1]; + final CSVReader src = new CSVReader(new FileReader(src_path)); + final CSVWriter dst = new CSVWriter(new FileWriter(dst_path)); + + String row[]; + while ((row = src.readNext()) != null) + dst.writeNext(concat(row, FOO)); + + dst.close(); + } +} diff --git a/README.txt b/README.txt index 1dd6cd9..a82d866 100644 --- a/README.txt +++ b/README.txt @@ -5,5 +5,8 @@ Setup: # compile crystal test crystal build --release cr-test.cr + # compile java test + javac -cp opencsv-2.3.jar JavaCSVTest.java + # compare tests ./bench.rb diff --git a/bench.rb b/bench.rb index 83f4113..ea983b3 100755 --- a/bench.rb +++ b/bench.rb @@ -7,6 +7,10 @@ Benchmark.bm(5) do |bm| `python2.7 ./py-test.py test.csv out.csv` end + bm.report('java') do + `java -cp opencsv-2.3.jar:. JavaCSVTest test.csv out.csv` + end + bm.report('ruby') do `ruby ./rb-test.rb test.csv out.csv` end diff --git a/opencsv-2.3.jar b/opencsv-2.3.jar new file mode 100644 index 0000000..31fcf30 Binary files /dev/null and b/opencsv-2.3.jar differ diff --git a/rb-test.rb b/rb-test.rb index 973c8cf..4bd2e57 100644 --- a/rb-test.rb +++ b/rb-test.rb @@ -5,8 +5,14 @@ require 'csv' # get paths src_path, dst_path = ARGV -CSV.open(dst_path, 'wb') do |dst| - CSV.foreach(src_path) do |row| - dst << (row += %w{foo} * 2) +File.open(dst_path, 'wb') do |dst_file| + CSV(dst_file) do |dst| + File.open(src_path, 'rb') do |src_file| + CSV(src_file) do |src| + src.each do |row| + dst << row + (%w{foo} * 2) + end + end + end end end diff --git a/results.txt b/results.txt index 4b530e6..9d1efd3 100644 --- a/results.txt +++ b/results.txt @@ -1,5 +1,5 @@ -pabs@meh:~/git/csv-test> ./bench.rb user system total real -python 0.000000 0.000000 15.620000 ( 15.862860) -ruby 0.000000 0.000000 139.730000 (140.269987) -crystal 0.000000 0.000000 13.300000 ( 13.153701) +python 0.000000 0.000000 15.170000 ( 15.388455) +java 0.000000 0.000000 7.680000 ( 7.818470) +ruby 0.000000 0.000000 132.520000 (133.027950) +crystal 0.000000 0.000000 13.530000 ( 13.249648) -- cgit v1.2.3