in reply to Subroutine overhead in Perl

Replace your grid_check() function with:
sub grid_check { my @f = grep { !$grid[$_] } 0 ..80; unless (@f) { return sudoku_print(); } my @min = (1..9); my $idx = 0; for my $cntr (@f) { my %p = map { $_ => undef } 1 .. 9; foreach my $cell (@{$search{$cntr}}) { delete $p{ $grid[$cell] }; } my @v = keys %p; if (@min >= @v) { @min = @v; $idx = $cntr; } } foreach my $val ( @min ) { $grid[$idx] = $val; grid_check(); } return $grid[$idx] = 0; }
The problem? You were calculating waaaay too much for each iteration, particularly since you threw away most of it anyways. This much better represents what you were trying to do. The results? For the devilish one, I went from 37.XX seconds to 1.XX seconds on my Macbook Pro. It's actually faster than the other hard one (2.7X seconds for me). That just has to do with path selection. It could be sped up about another 5-10% with a couple little things here and there, but I think this is the major improvement.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Subroutine overhead in Perl
by enemyofthestate (Monk) on Nov 08, 2007 at 05:53 UTC
    Thanks! That speeds things up a lot.

    I was glad to see where I was wasting clock cycles. I had no issue with a compiled language like Java being faster than an interpreted one like Perl and I'm very happy to see my inital estimate of the overheasd in a subroutine call in perl was too high by about three orders of magnitude.

    Phew!

      By interpreted, you of course mean "JIT-compiled". Because Perl is not interpreted...