Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check string with index before substitute #84

Merged
merged 1 commit into from
Oct 6, 2018
Merged

Conversation

atoomic
Copy link
Collaborator

@atoomic atoomic commented Oct 4, 2018

This is a micro optimization to speedup the
quotes stripping.

From a simple Benchmark::Dumb test it appears to be 25 to 35 %
faster. View GitHub case for benchmark script.

Here is a naive benchmark test proving the speedup advantage of the index usage vs the substr one liner.

#!perl

use strict;
use warnings;

use Benchmark::Dumb qw(:all);

my @noquotes = qw{some values without quotes};
my @quotes   = qw{'with' 'a' 'quote' 'longworwithaquote'};

for ( 1..10 ) {
	push @noquotes, @noquotes;
	push @quotes, @quotes;	
}

my @mix = @quotes, @noquotes;

print "# Strings with quotes\n";
compare( \@quotes );

print "# Strings without quotes\n";
compare( \@noquotes );

print "# A mixed of both: 50/50\n";
compare( \@mix );

sub compare {
	my ( $input ) = @_;

	cmpthese(
	    0.01,    # 1% precision
	    {   substitute => sub {
	    		my @values = ( @$input );

	    		foreach my $key ( @values ) {
	    			$key =~ s/^'(.+)'$/$1/s;	
	    		}

	            return;
	        },
	        index => sub {
	        	
	        	my @values = ( @$input );

	        	foreach my $key ( @values ) {
		            if ( index( $key, q['] ) == 0 ) {
		                substr( $key, 0,  1, '' );
		                substr( $key, -1, 1, '' );
		            }
	        	}
	            return;
	        },
	    }
	);

}

__END__

# Strings with quotes
                     Rate  substitute  index
substitute 297.31+-0.31/s          -- -34.2%
index      451.64+-0.77/s 51.91+-0.3%     --
# Strings without quotes
                    Rate   substitute  index
substitute  982.8+-3.9/s           -- -23.4%
index      1282.9+-4.4/s 30.53+-0.68%     --
# A mixed of both: 50/50
                     Rate   substitute  index
substitute 297.09+-0.61/s           -- -34.3%
index        451.9+-1.2/s 52.11+-0.51%     --

@atoomic atoomic force-pushed the ident-index branch 3 times, most recently from d94f17b to 082b7b9 Compare October 5, 2018 19:22
This is a micro optimization to speedup the
quotes stripping.

From a simple Benchmark::Dumb test it appears to be 25 to 35 %
faster. View GitHub case for benchmark script.
@toddr toddr merged commit 0b59549 into abw:master Oct 6, 2018
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Nov 24, 2018
Upstream changes:
Version 2.28 - 11th October 2018
#------------------------------------------------------------------------

* Add and enable Travis CI to track GitHub Pull Requests

* Template is now using GitHub as the official Bug Tracker

* Nicolas R. fixed a circular reference in Template::Plugin::Filter
  abw/Template2#152

* Nicolas R. adjusted group regexes to not be greedy
  abw/Template2#94

* Nicolas R. added unit tests to cover regression from RT 91172
  abw/Template2#122

* Nicolas R. added support for template files having mtime=0
  abw/Template2#102

* Todd Rinaldo fixed rand calls with no args in Math plugin
  abw/Template2#155

* Todd Rinaldo corrected ttree 2.22 logic change
  abw/Template2#148

* Todd Rinaldo turned off automated testing for tests using optional modules
  abw/Template2#156

* Nicolas R. adjusted unit tests to not force Stash::XS

* Nicolas R. added a pre allocated buffer in Stash.xs to avoid malloc/free
  abw/Template2#82

* Nicolas R. optmized Template::Parser by avoiding a dummy sub
  abw/Template2#83

* Nicolas R. optimized Template:Directive by using index
  abw/Template2#84

* Nicolas R. adjust _dotop logic in Stash for perl 5.28 and earlier
  abw/Template2#81

* Todd Rinaldo documented VMethod method called 'item'
  abw/Template2#90

* Nicolas R. adjusted t/filter.t after recent switch to RFC3986
  abw/Template2#179

* Nicolas R. fixed warnings from t/cgi.t
  abw/Template2#178

* Ivan Krylov added STRICT option to ttree
  abw/Template2#81

* Kent Fredric fixed relative path handling in templates on Perl 5.26+
  abw/Template2#80

* Tom Delmas fixed some typo from documentation
  abw/Template2#76

* Matthew Somerville switched uri/url to use RFC3986
  updated the documentation to match the history.
  abw/Template2#35

* Sebastien Deseille used remove_tree helper to remove directories
  abw/Template2#67

* Nick Hibma - Add Sortkeys to DUMPER_ARGS
  abw/Template2#64

* E. Choroba added a warn on duplicate block name
  abw/Template2#61

* Jason Lewis fixed some typo in ttree.pod
  abw/Template2#58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants