in reply to Re: Remove double bracket and singe quotes
in thread Remove double bracket and singe quotes
I came across FFI::TinyCC and FFI::Platypus recently. The Alien::TinyCC module is what compiles the C code and does so very quickly in memory.
This demonstration completes in 1.832 seconds, ahead of tr.
use strict; use warnings; use Time::HiRes qw( time ); # ------------------------------------------------------------------- use FFI::TinyCC; use FFI::Platypus::Declare qw( string int ); my $tcc = FFI::TinyCC->new; $tcc->compile_string (q{ int filter_str ( char* str ) { char* p = str; int i = 0; /* strip chars in-place */ while ( *str ) { if ( *str == '[' || *str == ']' || *str == '\'' ) { str++; continue; } *p++ = *str++; i++; } return i; } }); my $address = $tcc->get_symbol('filter_str'); attach [ $address => 'filter_str' ] => [ string ] => int; # ------------------------------------------------------------------- my $doc = "'C-3PO' or 'See-Threepio' is a humanoid robot character fro +m the [[Star Wars]] universe who appears in the original ''Star Wars' +' films, the prequel trilogy and the sequel trilogy.\n"; $doc .= $doc for 1 .. 22; ## expand string to 724 MB my $doclen = length $doc; print "length : $doclen\n"; ## 759169024 my $start = time; my $newlen = filter_str($doc); ## resize string to its new length substr $doc, $newlen, $doclen - $newlen, ''; printf "duration : %7.03f secs.\n", time - $start; print "length : $newlen\n"; ## 708837376
Well, curiosity got to me and the reason for trying TinyCC for the first time :)
|
|---|