A coworker claimed that "Java could beat Perl on any platform in this simple test: read a file in to a variable, write the contents of the variable to another file", so I decided to take him up on the challenge. I use Red Hat Linux 7.0 on a Pentium III 500Mhz; Perl 5.005_03, Sun's Java 1.3.1.

Here's the Perl I used:
#!/usr/bin/perl -w use Time::HiRes qw( time ); undef $/; $| = 0; my $file; my $start = time(); open(FILE, $ARGV[0]); $file = <FILE>; close FILE; open(FILE, ">$ARGV[1]"); print FILE $file; close FILE; my $total = time() - $start; print "$total Seconds Elapsed.\n";
Some sample runs:
[adamm@pepsi java_vs_perl]$ dd if=/dev/zero of=ten_meg_file.txt seek=1 +0 count=1 bs=1M 1+0 records in 1+0 records out [adamm@pepsi java_vs_perl]$ perl -w cp.pl ten_meg_file.txt out 0.405324935913086 Seconds Elapsed. [adamm@pepsi java_vs_perl]$ perl -w cp.pl ten_meg_file.txt out 0.322497963905334 Seconds Elapsed. [adamm@pepsi java_vs_perl]$ perl -w cp.pl ten_meg_file.txt out 0.321141004562378 Seconds Elapsed. [adamm@pepsi java_vs_perl]$ perl -w cp.pl ten_meg_file.txt out 0.318119049072266 Seconds Elapsed.
I realize that subsequent reads may be cached, resulting in faster times.

And the Java (sorry, I know this isn't Javamonks):
import java.io.*; public class X { public static void copy(String from, String to)throws IOException{ InputStream inFile = null; OutputStream outFile = null; try { inFile = new FileInputStream(from); outFile = new FileOutputStream(to); int length = inFile.available(); byte[] bytes = new byte[length]; inFile.read(bytes); outFile.write(bytes); } finally { inFile.close(); outFile.close(); } } public static void copyBuf(String from, String to)throws IOException +{ InputStream in = null; OutputStream out = null; InputStream inFile = null; OutputStream outFile = null; try { inFile = new FileInputStream(from); in = new BufferedInputStream(inFile); outFile = new FileOutputStream(to); out = new BufferedOutputStream(outFile); int length = in.available(); byte[] bytes = new byte[length]; in.read(bytes); out.write(bytes); } finally { in.close(); out.close(); } } public static void main(String[] args) { try { long l = System.currentTimeMillis(); copy(args[0], args[1]); System.out.println("Time = "+ (System.currentTimeMillis()-l)); l = System.currentTimeMillis(); copyBuf(args[0], args[1]); System.out.println("Buf Time = "+ (System.currentTimeMillis()-l) +); } catch (IOException e) { e.printStackTrace(); } } }
And some sample runs:
[adamm@pepsi java_vs_perl]$ javac X.java [adamm@pepsi java_vs_perl]$ dd if=/dev/zero of=ten_meg_file__2.txt see +k=10 count=1 bs=1M 1+0 records in 1+0 records out [adamm@pepsi java_vs_perl]$ java X ten_meg_file__2.txt out__2 Time = 1205 Buf Time = 815 [adamm@pepsi java_vs_perl]$ java X ten_meg_file__2.txt out__2 Time = 722 Buf Time = 838 [adamm@pepsi java_vs_perl]$ java X ten_meg_file__2.txt out__2 Time = 814 Buf Time = 779
These times are in milliseconds, and indicate that Perl was about twice as fast as Java at this simple operation. So now, my questions.

Thanks!

---
"A Jedi uses the Force for knowledge and defense, never for attack."

In reply to Java vs. Perl file I/O benchmarks; valid test? by meonkeys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.