in reply to Fastest byteswap (little endian to big endian (eg. 34127856 -> 12345678)

This runs in 0.02s on my machine, compared with your original which runs in 0.3s. So 15x faster :-)

my($data, $n); my $bufsize = 4096; my $mask0 = "\xff\0" x ($bufsize / 2 + 1); my $mask1 = "\0\xff" x ($bufsize / 2 + 1); $data = "\0"; while (($n = read $file, $data, $bufsize, 1) != 0) { print $reversedFile substr(((substr($data,2) & $mask0) | ($data & $mask1)), 0, $n) +; }

Dave.

  • Comment on Re: Fastest byteswap (little endian to big endian (eg. 34127856 -> 12345678)
  • Download Code

Replies are listed 'Best First'.
Re^2: Fastest byteswap (little endian to big endian (eg. 34127856 -> 12345678)
by ikegami (Patriarch) on Apr 14, 2015 at 12:50 UTC
    Shouldn't that be substr($data,1) (skip the NUL)?
      I see you've already corrected your post, but for anyone else wondering: even bytes have to be shifted left one slot; odd ones right one slot; the difference between -1 and +1 is 2, not 1.

      Dave.