I have a few lines of code I use for byte reversal of some 16mb files. I have got it to convert this byte order in .6 - .8 secs. I was wondering if there is a more faster way than using these two methods I have below. Please feel free to crush my record of 0.68640184402465.
Here is a file to test with ofcourse:
Test File
use strict;
use warnings;
use Time::HiRes qw( time );
my $start = time();
open (my $file, '<', $ARGV[0]) or die 'cannot open $file: $!';
binmode($file);
open (my $reversedFile, '+>', "$ARGV[0].swap");
binmode($reversedFile);
my($data, $n);
while (($n = read $file, $data, 4096) != 0) {
print $reversedFile pack("v*", unpack("n*", $data));
}
my $end = time();
my $runtime = sprintf("%.16s", $end - $start);
print $runtime;
And another method which is a little slower it seems:
use strict;
use warnings;
use Time::HiRes qw( time );
my $start = time();
my $input_file = $ARGV[0];
my $data = do {
local $/ = undef;
open (my $fh, "<", $input_file) or die "could not open $input_file
+: $!";
binmode($fh);
<$fh>;
};
my $reversed_data = pack( "v*", unpack( "n*", $data ) );
open my $output, '>', "bkpps3.swap.bin";
binmode($output);
print $output $reversed_data;
my $end = time();
my $runtime = sprintf( "%.16s", $end - $start );
print $runtime;
Like i said, I really am not to sure how to make it faster than it is. Any input is appreciated :)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.