Skip to main content
Tweeted twitter.com/StackMma/status/1313358346871767041
Became Hot Network Question
added 25 characters in body
Source Link
azerbajdzan
  • 25.1k
  • 2
  • 22
  • 62

I have noticed that many functions that are meant to operate on string are slower compared to similar functions that operate on lists.

One example: Counts versus LetterCounts (and notice that the version with Counts has to do two more tasks than version with LetterCounts - it has to do ToCharacterCode and then KeyMap with FromCharacterCode to transform the output to the LetterCounts version)

SeedRandom[1]
str = RandomInteger[{1, 26}, 300] /. 
    Thread[Range[26] -> CharacterRange["A", "Z"]] // StringJoin;

LetterCounts[str, 2] // RepeatedTiming

KeyMap[FromCharacterCode, 
  Sort[Counts[Partition[ToCharacterCode[str], 2, 1]], 
   Greater]] // RepeatedTiming

(*{0.00426, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)
(*{0.000830, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)

The speed difference is significant!

Something is rotten in the state of Denmark Wolfram.

I have noticed that many functions that are meant to operate on string are slower compared to similar functions that operate on lists.

One example: Counts versus LetterCounts (and notice that the version with Counts has to do two more tasks than version with LetterCounts - it has to do ToCharacterCode and then KeyMap to transform the output to the LetterCounts version)

SeedRandom[1]
str = RandomInteger[{1, 26}, 300] /. 
    Thread[Range[26] -> CharacterRange["A", "Z"]] // StringJoin;

LetterCounts[str, 2] // RepeatedTiming

KeyMap[FromCharacterCode, 
  Sort[Counts[Partition[ToCharacterCode[str], 2, 1]], 
   Greater]] // RepeatedTiming

(*{0.00426, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)
(*{0.000830, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)

The speed difference is significant!

Something is rotten in the state of Denmark Wolfram.

I have noticed that many functions that are meant to operate on string are slower compared to similar functions that operate on lists.

One example: Counts versus LetterCounts (and notice that the version with Counts has to do two more tasks than version with LetterCounts - it has to do ToCharacterCode and then KeyMap with FromCharacterCode to transform the output to the LetterCounts version)

SeedRandom[1]
str = RandomInteger[{1, 26}, 300] /. 
    Thread[Range[26] -> CharacterRange["A", "Z"]] // StringJoin;

LetterCounts[str, 2] // RepeatedTiming

KeyMap[FromCharacterCode, 
  Sort[Counts[Partition[ToCharacterCode[str], 2, 1]], 
   Greater]] // RepeatedTiming

(*{0.00426, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)
(*{0.000830, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)

The speed difference is significant!

Something is rotten in the state of Denmark Wolfram.

Source Link
azerbajdzan
  • 25.1k
  • 2
  • 22
  • 62

What is wrong with function `LetterCounts` and other functions that operate on strings?

I have noticed that many functions that are meant to operate on string are slower compared to similar functions that operate on lists.

One example: Counts versus LetterCounts (and notice that the version with Counts has to do two more tasks than version with LetterCounts - it has to do ToCharacterCode and then KeyMap to transform the output to the LetterCounts version)

SeedRandom[1]
str = RandomInteger[{1, 26}, 300] /. 
    Thread[Range[26] -> CharacterRange["A", "Z"]] // StringJoin;

LetterCounts[str, 2] // RepeatedTiming

KeyMap[FromCharacterCode, 
  Sort[Counts[Partition[ToCharacterCode[str], 2, 1]], 
   Greater]] // RepeatedTiming

(*{0.00426, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)
(*{0.000830, <|"TT" -> 3, "IF" -> 3, "EP" -> 3, ... , "HA" -> 1, "AH" -> 1, "FA" -> 1|>}*)

The speed difference is significant!

Something is rotten in the state of Denmark Wolfram.