#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw( time ); my $qfn = 'file'; { open(my $fh_out, '>', $qfn) or die; binmode($fh_out); for (1..1_000_000) { # Limit the ID in order to get collisions. print $fh_out (pack('I', rand(65536))); print $fh_out (join '', map chr(rand(255)), 1..4); } } my $stime = time; { open(my $fh_in, '<', $qfn) or die; binmode($fh_in); my %hash = (); while (read($fh_in, my $packed_data = '', 8)) { my ($id, $val) = unpack 'II', $packed_data; push @{$hash{$id}}, $val; } } my $etime = time; print($etime-$stime, "\n"); unlink($qfn) or warn;