Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

distance[str1_String,str2_String] := Module[{split,r},
    r=N@Total[Boole@StringMatchQ[str2,#,IgnoreCase-> True]&/@split]/Length@split    

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



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. – DavidC Aug 12 '13 at 16:04
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. – DavidC 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


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

Browse other questions tagged or ask your own question.