Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm using this distance function to compare distance between strings:

distance[str1_String,str2_String] := Module[{split,r},
    split="*"<>#<>"*"&/@StringSplit@str1;
    r=N@Total[Boole@StringMatchQ[str2,#,IgnoreCase-> True]&/@split]/Length@split    
]
totalDistance[str1_String,str2_String]:=(distance[str1,str2]+distance[str2,str1])/2

totalDistance works just nice, but it's one order of magnitude slower than native distance functions like EditDistance.

Here is some code test:

testList=ConstantArray["Distance String Test with Mathematica",50000];
totalDistance["String distance test with MMA",#]&/@testList//AbsoluteTiming//First
EditDistance["String distance test with MMA",#]&/@testList//AbsoluteTiming//First

4.063474

0.433012

Some clue on how to optimize this code?

share|improve this question
    
When I run your code, I get timings of 0.036450 and 0.823406, respectively! In other words, totalDistance, on my machine, runs 25X as fast as EditDistance. –  David Carraher Aug 12 '13 at 16:04
1  
I get the same as the OP on OS X, MMA v.9. –  Pickett Aug 12 '13 at 16:17
    
I just copied the code again and get my original result. There was an error in totalDistance that I had corrected some minutes ago. Maybe was it. –  Murta Aug 12 '13 at 16:23
    
Strange. I continue to get the timings I reported on 2 hours ago, using your updated code. MMA v. 9.01 OS X. –  David Carraher Aug 12 '13 at 18:18
    
Might be a difference between 9.0.1 and 9.0.0 then. –  Pickett Aug 12 '13 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.