in reply to Removal of values in array from array list

There is a significant time difference between methods 1 and 2 which is presumably because of the inner loop encoded explicitly in perl in method (2) vs the implicit inner loop in method (1) actually implemented in C.

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Time::HiRes qw(time); use Data::Dump qw(dump pp); my %checkFrom; my @toCheck; sub init0() {@toCheck = (); push @toCheck, rand(1e6) for 1..1e4; } sub init1() {%checkFrom = (); for my $x(0..999) {for my $y(0..999) {$checkFrom{$x}[$y] = 1e3*$x+$y; } } init0(); } sub init2() {%checkFrom = (); for my $x(0..999) {for my $y(0..999) {$checkFrom{$x}{$y} = 1e3*$x+$y; } } init0(); } for(1..10) {if (1) {init1(); my $s = time(); {my %toCheck = map { $_ => 1 } @toCheck; @$_ = grep !$toCheck{$_}, @$_ for values %checkFrom; } say "1 took ", (time() - $s); } if (2) {init2(); my $s = time(); {for my $thing (@toCheck) {for my $category (keys %checkFrom) {delete $checkFrom{$category}{$thing}; } } } say "2 took ", (time() - $s); } }

Produces

1 took 1.06206107139587
2 took 11.8416769504547
1 took 1.10106301307678
2 took 12.3792278766632
1 took 1.07640194892883
2 took 12.4490790367126
1 took 1.2350709438324
2 took 12.7647299766541
1 took 1.27407312393188
2 took 12.8677358627319
1 took 1.17906808853149
2 took 12.6504328250885
1 took 1.17000198364258
2 took 12.8158860206604
1 took 1.2890739440918
2 took 12.8577361106873
1 took 1.32807612419128
2 took 12.7377278804779
1 took 1.24107122421265
2 took 12.7136778831482