in reply to Strange benchmark result that has me totally confounded
If you're interested in making this run faster, I'd suggest merging some of these regular expressions.
For example, if you build a hash of mappings, you could use something like the following: $post =~ s!(\:[\-\']?([Dp\[\(\)\$]))!<img src="./emoticons/$convert{$1}.gif">!gio;
Something along those lines should be able to double your performance.
Here's the script I used for benchmarking:
#!/usr/bin/perl use Benchmark qw/countit cmpthese/; # Load the messages array our @messages; while (<DATA>){ chomp; push(@messages, $_) if ($_ =~ m/:/); }; print STDERR "Array loaded with ", scalar @messages, " posts.\n"; sub run($) { countit(10, @_) } print "Begin benchmark.\n"; my %convert = ( '[' => 'bat.gif', 'D' => 'bigsmile.gif', "'(" => 'cry.gif', ')' => 'smile.gif', '(' => 'sad.gif', '$' => 'sour.gif', 'p' => 'tounge.gif', '\\' => 'what.gif', ); my $convert = join ("|", map "\Q$_", keys %convert); cmpthese { should_be_fastest => run q{ my @messages = @messages; foreach my $post (@messages){ $post =~ s!\:\-?($convert)!<img src="./emoticons/$convert{$1}.gi +f">!gio; }}, should_be_faster => run q{ my @messages = @messages; foreach my $post (@messages){ if ($post =~ m/:/) { $post =~ s!\Q:-[\E!<img src="./emoticons/bat.gif">!gio; $post =~ s!\Q:[\E!<img src="./emoticons/bat.gif">!gio; $post =~ s!\Q:-D\E!<img src="./emoticons/bigsmile.gif">!gio; $post =~ s!\Q:D\E!<img src="./emoticons/bigsmile.gif">!gio; $post =~ s!\Q:'(\E!<img src="./emoticons/cry.gif">!gio; $post =~ s!\Q:-)\E!<img src="./emoticons/smile.gif">!gio; $post =~ s!\Q:)\E!<img src="./emoticons/smile.gif">!gio; $post =~ s!:\(!<img src="./emoticons/sad.gif">!gio; $post =~ s!:-\(!<img src="./emoticons/sad.gif">!gio; $post =~ s!\Q:-$\E!<img src="./emoticons/sour.gif">!gio; $post =~ s!\Q:$\E!<img src="./emoticons/sour.gif">!gio; $post =~ s!\Q:-p\E!<img src="./emoticons/tounge.gif">!gio; $post =~ s!\Q:p\E!<img src="./emoticons/tounge.gif">!gio; # $post =~ s!\Q:-\\\E!<img src="./emoticons/what.gif">!gio; # $post =~ s!\Q:\\\E!<img src="./emoticons/what.gif">!gio; }}}, should_be_slower => run q{ my @messages = @messages; foreach my $post (@messages){ if ($post =~ m/:/){ $post =~ s!\Q:-[\E!<img src="./emoticons/bat.gif">!gio; $post =~ s!\Q:[\E!<img src="./emoticons/bat.gif">!gio; $post =~ s!\Q:-D\E!<img src="./emoticons/bigsmile.gif">!gio; $post =~ s!\Q:D\E!<img src="./emoticons/bigsmile.gif">!gio; $post =~ s!\Q:'(\E!<img src="./emoticons/cry.gif">!gio; $post =~ s!\Q:-)\E!<img src="./emoticons/smile.gif">!gio; $post =~ s!\Q:)\E!<img src="./emoticons/smile.gif">!gio; $post =~ s!:\(!<img src="./emoticons/sad.gif">!gio; $post =~ s!:-\(!<img src="./emoticons/sad.gif">!gio; $post =~ s!\Q:-$\E!<img src="./emoticons/sour.gif">!gio; $post =~ s!\Q:$\E!<img src="./emoticons/sour.gif">!gio; $post =~ s!\Q:-p\E!<img src="./emoticons/tounge.gif">!gio; $post =~ s!\Q:p\E!<img src="./emoticons/tounge.gif">!gio; $post =~ s!\Q:-\\\E!<img src="./emoticons/what.gif">!gio; $post =~ s!\Q:\\\E!<img src="./emoticons/what.gif">!gio; }}}} ; warn "Finished with $#messages\n"; __DATA__ We'll never get this fixed :wink: i would like to see that :) and yaaay windows patches :) it got approved at 6:30 auditor: could you please approve a shift i bid on in m160 tonight? :-D i told her the only thing I thought is that she could let it sit overn +ight and g +et it tomorrow morning at 7:30..but then she has to leave her externa +l hard dri +ve appreciate ur help:D or you can use the form located here: Auditor: Will you approve Peters bid for my shift? hello, :) or good evening as it goes it warnt to shabby from the third floor :wink: I have no windows :( http://theonion.com/news/index.php?issue=4015&n=2 im slow :) please do tell :D Nicholas --the hard worker :) good Job, stacy....:) need :coffee: here in PY 120 :love: Hey kids! I'll see ya later! :) http://registrar.iupui.edu/enrollment/203/final203.html Blair: I got a response about Finals scheduling Awww! You can make your OSX dock look like it's growing grass! http:// +www.apple. +com/downloads/macosx/icons_screensavers/docklawnicons.html :) :jacob: I agree. I always turn it off when I work in labs. :) i hate that group similar icons on taskbar is defaulted :rant: Done :) :) TJ: The plotter print worked on the first try. Good work. :) I wish I could take the last 18minutes of my break and just leave earl +y today :w +ink: Yep, The IC is a *constant* party. :-P What's up, :stc:? And yes, Heather, their username is: adl. sure, i bet that's what you meant :wink: Kellen: no 10:30, you can stay in as long as you want but you can't leave and co +me back B-school is only open until 10:3 TGIF :) i'm doing pretty well, glad that it's friday :) happy friday everyone :) That's all I got, sorry :) I'm confused don't mind me I haven't had my medicine today :) :wink: I was starting to worry about you :) when an observer or tt comes to the ed. school.. make sure you bring y +our staple + fixing skills with you :-) :) nevermind, found it :) :) Tristan: I find that remark offensive. afternoon all :) laterz everybody :) :squirrel: :pie: lol cuz i have a strange life it seems :-D got it :-D haha i yam! so so sooo innocent :kathy: anybody? anybody? pleaasse :-D :) lol sorry having fun with chat icons :-D i was going :yinli: I was scared :'( but i'm not one of 'em :) :) Dan: I can attest to the fact that South Central Boy was indeed in Wo +odburn Hal +l LOL perhaps :) dan you disbeliever :-P :) it's ok :) hope you feel better :) tired and not feeling so good :( but yeah :-D how's everybody doin? :( lol no i was just talking to my parents--apparently they are going to +atlantic c +ity for a daytrip :-/ oh yes it was :) oh, it is too late andy... he cried. :( and it sounds like that is what his next move will be: declare himself + insane so + he won't have to go back to prison http://talk.assmotax.org/index.php Good Morning :stc: todays theme is secret spynames for stc coworkers : +) secret Austin man :) da nana na na .. da na na Big A :) :D oh no Big A is taking over the chat.. ahhh run for your lives.. no one + has every + escaped his wrath :) woah.. blank slate be the chat now :)
|
|---|