在 J 中使用符号而不是字符串的启发式方法

Heuristic for using symbols rather than strings in J

虽然使用 J 的符号 (s: ' Abe Bill Chad') 而不是字符串数组 ('Abe','Bill',:'Chad') 或字符串的盒装列表 ('Abe';'Bill';'Chad') 的真正原因是它是最好的解决方案(大多数人或机器 efficient/convenient),何时使用符号的经验法则是什么?

s:vocabulary page 提到高效 "searching, sorting, and comparisons." 如果您有任何机会进行搜索、排序或比较,您会从头开始使用符号吗?还是仅在您认识到您的代码正在解决其他选项的限制时才使用符号?或者两者之间有一些细微差别?

我的经验是,对于大多数用例,符号 (s:) 并不是提供可接受的性能所必需的,这与 section on Symbol in J for C Programmers 中的建议相符,如果您发现您的程序需要一个很多时间匹配字符串。它还警告说:

there is no way to tell the interpreter to free the resources for a single string; this can be a problem if your symbol table is large and changes dynamically.

因此,Vocabulary page for Symbol on JWiki 建议符号在以下情况下最有用:

  • 有限数量的字符串重复出现
  • 符号集已知且不变