Home
java vs. ruby amateur benchmarks

Listed below are some amateur benchmark comparisons between the Java and Ruby programming languages. Instead of using complex mathematical computations, I decided to use simple, every day, business-like programming "stuff" for these benchmarks; this included: looping, file i/o, socket/http, threads, and database access.

One additional factor I wanted to compare was the code size, so I coded the two programs to look as similiar as possible (e.g. similar class/method names). In addition, I avoided unnecessary classes and keywords (e.g. method access keywords in Java) but at the same time, I avoided fancy shortcuts that can make code somewhat more difficult to follow (e.g. PERL like syntax in Ruby).

The two benchmark programs below are somewhat quick-and-dirty, so if you find any flaws and/or have any suggestions for improvements, please fell free to contact me via email: anil <at> visualpatterns.com. I hope these benchmarks are helpful to you.

Cheers,
Anil Hemrajani
March, 2007

PS. If you are interested in more thorough benchmarks between these and many other programming languages, check out the Gentoo : Intel® Pentium® 4 Computer Language Shootout.

Hardware/OS: Dell Inspiron 6000, Intel Pentium MProcessor 750 (1.86GHz), 1 GB memory, Windows XP Professional
  Java Ruby
Version > java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
> ruby --version
ruby 1.8.5 (2006-08-25) [i386-mswin32]
Code
Size
> wc Benchmarks.java
136 536 5400 Benchmarks.java
> wc benchmarks.rb
117 363 3097 benchmarks.rb
Java
Code
Ruby Code
Run
#1
Results
> java -cp mysql.jar;. Benchmarks
17:37.57.324 > PROGRAM START
17:37.57.356 > testThreads start
17:37.57.403 > testThreads end - 0.031 sec
17:37.57.403 > testLoop start
17:37.57.606 >   looped 100000000 times
17:37.57.606 > testLoop end - 0.203 sec
17:37.57.606 > testFileIO start
17:38.03.559 >   1000000 lines read from javatestfileio.txt
17:38.03.559 > testFileIO end - 5.953 sec
17:38.03.559 > testSocket start
17:38.05.871 >   1077827 bytes downloaded to javatestsocket.pdf
17:38.05.871 > testSocket end - 2.312 sec
17:38.05.871 > testDatabase start
17:38.07.153 >   insert start
17:41.56.387 >   insert end
17:41.56.387 >   fetch start
17:41.56.481 >   fetch end (10000 rows found)
17:41.56.481 >   delete start
17:41.56.699 >   delete end
17:41.56.809 > testDatabase end - 230.938 sec
17:41.56.809 > PROGRAM END - 239.672 sec
> ruby benchmarks.rb
17:32:18.574 > PROGRAM START
17:32:18.574 > test_threads start
17:32:18.574 > test_threads end - 0.0 sec
17:32:18.574 > test_loop start
17:32:32.356 >   looped 100000000 times
17:32:32.356 > test_loop end - 13.782 sec
17:32:32.356 > test_fileio start
17:32:48.856 >   1000000 lines read from rubytestfileio.txt
17:32:48.856 > test_fileio end - 16.5 sec
17:32:48.856 > test_socket start
17:32:49.434 >   1077827 bytes downloaded to rubytestsocket.pdf
17:32:49.434 > test_socket end - 0.578 sec
17:32:49.434 > test_database start
17:32:49.903 >   insert start
17:37:05.543 >   insert end
17:37:05.543 >   fetch start
17:37:05.637 >   fetch end (10000 rows found)
17:37:05.637 >   delete start
17:37:06.278 >   delete end
17:37:06.387 > test_database end - 256.953 sec
17:37:06.387 > PROGRAM END - 287.813 sec
Run
#2
Results
> java -cp mysql.jar;. Benchmarks
18:14.34.246 > PROGRAM START
18:14.34.246 > testThreads start
18:14.34.293 > testThreads end - 0.047 sec
18:14.34.293 > testLoop start
18:14.34.403 >   looped 100000000 times
18:14.34.403 > testLoop end - 0.11 sec
18:14.34.403 > testFileIO start
18:14.40.137 >   1000000 lines read from javatestfileio.txt
18:14.40.137 > testFileIO end - 5.734 sec
18:14.40.137 > testSocket start
18:14.41.028 >   1077827 bytes downloaded to javatestsocket.pdf
18:14.41.028 > testSocket end - 0.891 sec
18:14.41.028 > testDatabase start
18:14.41.528 >   insert start
18:19.20.481 >   insert end
18:19.20.481 >   fetch start
18:19.20.574 >   fetch end (10000 rows found)
18:19.20.574 >   delete start
18:19.21.262 >   delete end
18:19.21.356 > testDatabase end - 280.328 sec
18:19.21.356 > PROGRAM END - 287.141 sec
> ruby benchmarks.rb
18:19:25.403 > PROGRAM START
18:19:25.403 > test_threads start
18:19:25.418 > test_threads end - 0.015 sec
18:19:25.418 > test_loop start
18:19:39.418 >   looped 100000000 times
18:19:39.418 > test_loop end - 14.0 sec
18:19:39.418 > test_fileio start
18:19:56.340 >   1000000 lines read from rubytestfileio.txt
18:19:56.340 > test_fileio end - 16.922 sec
18:19:56.340 > test_socket start
18:19:57.153 >   1077827 bytes downloaded to rubytestsocket.pdf
18:19:57.153 > test_socket end - 0.813 sec
18:19:57.153 > test_database start
18:19:57.699 >   insert start
18:24:22.871 >   insert end
18:24:22.871 >   fetch start
18:24:22.965 >   fetch end (10000 rows found)
18:24:22.965 >   delete start
18:24:23.199 >   delete end
18:24:23.309 > test_database end - 266.156 sec
18:24:23.309 > PROGRAM END - 297.906 sec
Run
#3
Results
> java -cp mysql.jar;. Benchmarks
19:56.10.484 > PROGRAM START
19:56.10.500 > testThreads start
19:56.10.562 > testThreads end - 0.046 sec
19:56.10.562 > testLoop start
19:56.10.687 >   looped 100000000 times
19:56.10.687 > testLoop end - 0.125 sec
19:56.10.687 > testFileIO start
19:56.16.437 >   1000000 lines read from javatestfileio.txt
19:56.16.437 > testFileIO end - 5.75 sec
19:56.16.437 > testSocket start
19:56.21.078 >   1077827 bytes downloaded to javatestsocket.pdf
19:56.21.078 > testSocket end - 4.641 sec
19:56.21.078 > testDatabase start
19:56.22.390 >   insert start
20:00.11.468 >   insert end
20:00.11.468 >   fetch start
20:00.11.562 >   fetch end (10000 rows found)
20:00.11.562 >   delete start
20:00.11.843 >   delete end
20:00.11.921 > testDatabase end - 230.843 sec
20:00.11.921 > PROGRAM END - 242.093 sec
> ruby benchmarks.rb
19:50:53.468 > PROGRAM START
19:50:53.468 > test_threads start
19:50:53.484 > test_threads end - 0.016 sec
19:50:53.484 > test_loop start
19:51:07.250 >   looped 100000000 times
19:51:07.250 > test_loop end - 13.766 sec
19:51:07.250 > test_fileio start
19:51:24.687 >   1000000 lines read from rubytestfileio.txt
19:51:24.687 > test_fileio end - 17.437 sec
19:51:24.687 > test_socket start
19:51:26.140 >   1077827 bytes downloaded to rubytestsocket.pdf
19:51:26.156 > test_socket end - 1.469 sec
19:51:26.156 > test_database start
19:51:27.780 >   insert start
19:55:09.296 >   insert end
19:55:09.296 >   fetch start
19:55:09.437 >   fetch end (10000 rows found)
19:55:09.437 >   delete start
19:55:09.671 >   delete end
19:55:09.796 > test_database end - 223.64 sec
19:55:09.796 > PROGRAM END - 256.328 sec
© <%= (new java.text.SimpleDateFormat("yyyy")).format(new java.util.Date()) %> Visual Patterns, Inc. All rights reserved.