[英]Ruby block comment and profile questions
I have written a Ruby version of Erik Demaine's (MIT) docdist8.py. 我已经写了Erik Demaine(MIT)docdist8.py的Ruby版本。 This is available on github as docdist-v3.rb .
这可以在github上作为docdist-v3.rb获得 。 I faced two weird kind of situations:
我遇到了两种奇怪的情况:
1) In the function inner_product there is a block comment: 1)在函数inner_product中有一个块注释:
Inner product between two vectors, where vectors
are repeated as dictionaries of (word, freq) pairs.
Example : inner_product({"and":3, "of":2, "the":5},
{"and":4, "in":1, "of":1, "this":2}) = 14.0
If I wrap this with =begin and =end there is no problem, but if I wrap it with triple double-quotes """, I get errors as follows: 如果我用= begin和= end包裹它,就没有问题,但是如果我用三重双引号“”“包裹,则会出现如下错误:
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:72: syntax error, unexpected kIN, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
..."and":4, "in":1, "of":1, "this":2}) = 14.0
^
Are there rules / allowed entries for """ that are different from =begin and =end? 是否有“ /”的规则/允许的条目与= begin和= end不同?
2) When I run my program with the time command it executes in about 0.3 seconds. 2)当我使用time命令运行程序时,它将在0.3秒内执行。 However, if I put require 'profile' the time it takes becomes very high in comparison - 30 seconds.
但是,如果我要求“配置文件”,则与之相比,它花费的时间会非常长-30秒。 Hence I don't get the correct output at all.
因此,我根本没有获得正确的输出。 This doesn't seem to be the case with the original Python version, where it takes only a marginal extra time to profile.
原始Python版本似乎不是这种情况,在原始Python版本中,仅需花费很少的额外时间即可进行配置。 How do I get the same profile run in Ruby?
如何在Ruby中运行相同的配置文件?
Note: The two files I used to run the Ruby program are t2.bobsey.txt and t3.lewis.txt. 注意:我用来运行Ruby程序的两个文件是t2.bobsey.txt和t3.lewis.txt。 They are available at http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm
它们可在http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm上找到。
1) Block comments always have the form: 1)块注释始终具有以下形式:
=begin
Comment
=end
You are actually creating a string that is never used: 您实际上正在创建一个从未使用过的字符串:
"""
Not a comment
"""
# => "\nNot a comment\n"
That's why you get an error when adding another quote and that's why the syntax highlighting renders them as strings. 这就是为什么在添加另一个引号时会出错,并且是语法高亮显示它们为字符串的原因。
2) It's slower with profiler but I get identical results: 2)使用事件探查器速度较慢,但得到的结果相同:
ruby docdist-v3.rb t2.bobsey.txt t3.lewis.txt
File t2.bobsey.txt:262111 lines,49785 words,3354 distinct words
File t3.lewis.txt:1015474 lines,182355 words,8530 distinct words
The distance between the documents is: 0.574160 (radians)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.