SPUG: Re: eval efficiency

Tim Maher/CONSULTIX tim at consultix-inc.com
Tue Dec 7 01:42:25 CST 1999


This got trapped by the Majordomo filter looking for Zubscribe/unZubscribe
(s/Z/s/g) requests. I put a <SP> before each  ^sub, in the hope that this
would let the post get through.

-Tim
==========================================================
|  Tim Maher, Ph.D.           Tel/Fax: (206) 781-UNIX    |
|  SPUG Founder & Leader      Email: spug at halcyon.com    |
|  Seattle Perl Users Group   HTTP: www.halcyon.com/spug |
==========================================================
----- Forwarded message from owner-spug-list at pm.org -----

From: owner-spug-list at pm.org
Date: Mon, 6 Dec 1999 21:47:00 -0500 (EST)
X-Authentication-Warning: happyfunball.pm.org: mjordomo set sender to owner-spug-list at pm.org using -f
To: owner-spug-list at pm.org
Subject: BOUNCE spug-list at pm.org:     Admin request of type /^sub\b/i at line 9  

>From tim at consultix-inc.com  Mon Dec  6 21:46:58 1999
Received: from rand.csyn.net (dsl016180.midsl.net [216.190.16.180])
	by happyfunball.pm.org (8.9.3/8.9.1) with ESMTP id VAA10682
	for <spug-list at pm.org>; Mon, 6 Dec 1999 21:46:54 -0500 (EST)
Received: from localhost (sean at localhost)
	by rand.csyn.net (8.9.3/8.9.3) with ESMTP id SAA89237;
	Mon, 6 Dec 1999 18:49:53 -0800 (PST)
	(envelope-from sean at perlgods.com)
Date: Mon, 6 Dec 1999 18:49:53 -0800 (PST)
From: Sean Chittenden <sean at perlgods.com>
X-Sender: sean at rand.csyn.net
To: Asim Jalis <ajalis at beryllium.cobaltgroup.com>
cc: spug-list at pm.org
Subject: Re: SPUG: Efficiency of Eval
In-Reply-To: <199912062238.OAA20433 at beryllium.cobaltgroup.com>
Message-ID: <Pine.BSF.4.20.9912061736060.88924-100000 at rand.csyn.net>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII

	I dunno, but here's how you'd find out (surprising results at the
end, favors eval):

#!/usr/bin/perl -w

use strict;
use Benchmark;

 sub DieEvalTest() {
  eval {
    open FILE, "</nonexistant_file" or die "Could not open /nonexistant_file: $!";
    close FILE;
  };

  if ($@) {
    my $Drat = "Could not open the file\n";
    return(1);
  } else {
    my $Success = "Could open the file\n";
    close FILE;
    return(0);
  }
}

 sub ErrorFlagTest() {
  my $Error = 0;
  open FILE, "</nonexistant_file" or $Error = 1;

  if ($Error) {
    my $Drat = "Could not open the file\n";
    return(1);
  } else {
    close FILE;
    my $Success = "Could open the file\n";
    return(0);
  }
}

timethese(10000000,
	  {
	   'ErrorFlag' => &DieEvalTest(),
	   'Eval/die' => &ErrorFlagTest(),
	  }
	 );
		    

# End of file

Output of Testing.pl:

18:06 95 sean testing > perl Testing.pl
Benchmark: timing 10000000 iterations of Eval/die, ErrorFlag...
  Eval/die:  3 wallclock secs ( 2.49 usr + -0.06 sys =  2.43 CPU)
 ErrorFlag:  2 wallclock secs ( 1.41 usr + -0.01 sys =  1.40 CPU)


	I'd say that the timing mechinism is a bit whonky, but I'd go with
variables instead of eval.  You could use pre-compiled code for the open:


[...  Before the DieEvalTest() ...]
# Cache Compiled Open bytecode 
 sub CCOBC() {
  open FILE, "</nonexistant_file" or die "Could not open
/nonexistant_file: $!";
  close FILE;
}

 sub DieEvalTest() {
  eval { &CCOBC() };

  if ($@) {
[...]


Output of Testing2.pl:

18:07 97 sean testing > perl Testing2.pl
Benchmark: timing 10000000 iterations of Eval/die, ErrorFlag...
  Eval/die:  2 wallclock secs ( 1.47 usr +  0.05 sys =  1.52 CPU)
 ErrorFlag:  2 wallclock secs ( 1.67 usr + -0.02 sys =  1.65 CPU)

	Caching the compiled open code drops almost a full second off of
the Eval/die code (which surprised me greatly!).  In most cases, however,
using the caching scheme above just creates code-bloat, especially since
it only saved you an extra .1 second speed increase with 10M iterations.  
For all intents and purposes, it's easier/quicker to use the ErrorFlag
method of dealing w/ things, however, if you want, you can tweak things
and get them faster through alternative means (die/eval).

	--Sean

--
Sean Chittenden					<sean at perlgods.com>
Want an e-mail alias?

On Mon, 6 Dec 1999, Asim Jalis wrote:

> Date: Mon, 6 Dec 1999 14:38:54 -0800
> From: Asim Jalis <ajalis at beryllium.cobaltgroup.com>
> To: spug-list at pm.org
> Subject: SPUG: Efficiency of Eval
> 
> Is:
> 
>   eval { some_function_which_calls_die_on_error ($a) ; <stuff> ; }
>   if ($@) { <handle-exception> ; }
> 
> more efficient, or is:
> 
>   $r = some_function_which_returns_non_zero_on_error ($a) ; 
>   if ($r != 0) { <handle-exception> }
>   else         { <stuff> . . . <stuff> ; }
> 
> more efficient (in terms of speed). (Ignore code typos.)
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
>         Email to majordomo at pm.org: ACTION spug-list your_address
> 
> 
> 






----- End forwarded message -----

-- 
*==================================================================*
| Tim Maher, PhD  Consultix &        (206) 781-UNIX/8649           |
|  Pacific Software Gurus, Inc       Email: tim at consultix-inc.com  |
|  UNIX/Linux & Perl Training        http://www.consultix-inc.com  |
| Classes: 12/13 Perl; 12/17 Adv. Pattern Matching; 1/18 Int. Perl |
*==================================================================*

----- End forwarded message -----

-- 
*==================================================================*
| Tim Maher, PhD  Consultix &        (206) 781-UNIX/8649           |
|  Pacific Software Gurus, Inc       Email: tim at consultix-inc.com  |
|  UNIX/Linux & Perl Training        http://www.consultix-inc.com  |
| Classes: 12/13 Perl; 12/17 Adv. Pattern Matching; 1/18 Int. Perl |
*==================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list