in reply to code to get the date for "next Thursday"

It might not be any faster, but you can also do it as follows:

use POSIX qw( strftime ); use Time::Local qw( timegm_nocheck ); my ($day, $mon, $year, $wday) = (localtime())[3..6]; # Defaults to now. $year += 1900; $day += (7 - $wday + 4) % 7; $next_th = timegm_nocheck(0,0,0,$day,$mon,$year); print(strftime('%Y-%m-%d', gmtime($next_th)), "\n");

Only core modules are used.

Update: Now using GMT functions for the date to avoid problems from daylight savings time. (This should speed things up a bit too!) The date is still in localtime since we used localtime to fetch the current date.

Update: I had two names for the same variable ($day and $mday). Fixed.

Replies are listed 'Best First'.
Re^2: code to get the date for "next Thursday"
by duckyd (Hermit) on Mar 20, 2006 at 21:15 UTC
    This method appears to be a little bit slower, though neither seem all that "slow" to me. 16+ million iterations per second on my little powerbook seems plenty fast...

    Are you sure this is the piece of your code that's slow?

    Rate ikegami code bfdi code ikegami code 16393443/s -- -20% bfdi code 20408163/s 24% --

      Well, the machine that I normally run this on takes 3-5 seconds to run the script. My current laptop runs this pretty quickly but it is also not the "production environment" for this script.

      I will take a look at some of the options as well as the test script to see which one will work best for the target system.

      Thanks!