Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Quicker way to compare these 2 strings?

by cormanaz (Deacon)
on May 19, 2010 at 00:14 UTC ( #840585=note: print w/replies, xml ) Need Help??


in reply to Quicker way to compare these 2 strings?

You could do the same thing with substr without creating the arrays:
my $matches; for my $i (0..length($first) { if (substr($first,$i,0) eq substr($second,$i,0)) { $matches++; } }
I don't know if it would be faster, but it's possible since you wouldn't have the overhead associated with splitting the strings into the arrays. Maybe some interpreter monk knows the answer to that.

Replies are listed 'Best First'.
Re^2: Quicker way to compare these 2 strings? (Benchmark)
by toolic (Bishop) on May 19, 2010 at 00:29 UTC
    I don't know if it would be faster, ... Maybe some interpreter monk knows the answer to that.
    No need to have intimate knowledge of the interpreter when the OP can just Benchmark the split solution versus your fine substr solution, using representative strings.
Re^2: Quicker way to compare these 2 strings?
by Anonymous Monk on May 19, 2010 at 00:29 UTC
    Ok, with some minor corrections it works just fine!
    Thanks!
    $first= "AAAAXXXXAAAAXXXXXXXXXXXAAAA"; $second="AXXXAXXAAXXXXXAAAAXXXXXAAAA"; $matches=0; for $i (0..length($first)-1) { if (substr($first,$i,1) eq substr($second,$i,1)) { $matches++; } } print $matches."\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://840585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-09-26 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?