use strict; use Data::Dumper; use Time::HiRes qw(time); print STDERR "Building cats\n"; my %cats = (); foreach my $one ('a'..'d') { $cats{"a.$one"} = "a.$one"; foreach my $two ('a'..'z') { $cats{"a.$one.$two"} = "a.$one.$two"; foreach my $three ('a'..'z') { $cats{"a.$one.$two.$three"} = "a.$one.$two.$three"; } } } my @keys = sort keys %cats; my $cnt = scalar @keys; print STDERR "Building array from $cnt possibilities\n"; my $n = 100000; my %ids = (); foreach my $id (0..$n) { my $rand = int(rand($cnt)); $ids{$id} = $cats{$keys[$rand]}; } @keys = keys %ids; $cnt = scalar @keys; my %ret = (); my $x = 100000; my $start_time = time(); print STDERR "Returning mapped categories for values from a pool of $cnt\n"; foreach my $id (0..$x) { my $cat = $ids{$id}; if(!$ret{$ids{$id}}{'count'}) { $ret{$ids{$id}}{'count'} = 0; $ret{$ids{$id}}{'head_count'} = 'head_count'; $ret{$ids{$id}}{'cat_count'} = 'cat_count'; $ret{$ids{$id}}{'subcat_count'} = 'subcat_count'; } $ret{$ids{$id}}{'count'}++; } @keys = keys %ret; $cnt = scalar @keys; my $end_time = time(); my $total_time = $end_time - $start_time; print STDERR "Built $cnt entries to start with, $total_time seconds\n"; while(my($key,$val) = each(%ret)) { my @parts = split(/\./, $key); my $id = ''; foreach my $add (@parts) { $id .= $add; if($id eq $key) { next; } if(!$ret{$id}{'count'}) { $ret{$id}{'count'} = 0; $ret{$id}{'head_count'} = 'head_count'; $ret{$id}{'cat_count'} = 'cat_count'; $ret{$id}{'subcat_count'} = 'subcat_count'; } $ret{$id}{'count'} += $ret{$key}{'count'}; $id .= '.'; } } $end_time = time(); $total_time = $end_time - $start_time; my @sorted_keys = sort keys(%ret); my $tot = $ret{'a'}{'count'}; print STDERR "Completed in $total_time seconds ($tot)\n"; foreach my $key (@sorted_keys) { my $cnt = $ret{$key}{'count'}; #print "KEY: $key $cnt\n"; } print STDERR "Sleeping...\n"; sleep(1000);