Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 什么';在Rebol中计算行数的最快/最有效的方法是什么?_String_Performance_Newline_Rebol - Fatal编程技术网

String 什么';在Rebol中计算行数的最快/最有效的方法是什么?

String 什么';在Rebol中计算行数的最快/最有效的方法是什么?,string,performance,newline,rebol,String,Performance,Newline,Rebol,给定一个字符串string,计算其中行数的最快/最有效的方法是什么?对于任何口味的Rebol,我们都会接受最佳答案。我一直在假设parse[some[thru]]组合是遍历字符串的最快方式,但我不确定这一点,因此转而考虑: count-lines: func [string [string!] /local count][ parse/all string [ (count: 1) some [thru newline (count: count + 1)] ]

给定一个字符串
string
,计算其中行数的最快/最有效的方法是什么?对于任何口味的Rebol,我们都会接受最佳答案。我一直在假设
parse[some[thru]]
组合是遍历字符串的最快方式,但我不确定这一点,因此转而考虑:

count-lines: func [string [string!] /local count][
    parse/all string [
        (count: 1) some [thru newline (count: count + 1)]
    ]
    count
]
或:

柜台呢?重复的效率有多高

count-lines: func [string [string!]][
    repeat count length? string [
        unless string: find/tail string newline [
            break/return count
        ]
    ]
]
更新:行计数遵循文本编辑器的原则:

空文档的行数仍为1。因此:

>> count-lines ""
== 1
>> count-lines "^/"
== 2

以下是最适合我的:

temp: read/lines %mytext.txt
length? temp

不了解性能和最后一行规则(r3)


删除每个文件可以很快,因为它是本机文件

s: "1^/2^/3"
a: length? s
print a - length? remove-each v s [v = #"^/"]
; >> 2
或者作为一个函数

>> f: func [s] [print [(length? s) - (length? remove-each v s [v = #"^/"])]]
>> f "1^/2^/3"
== 2

BrianH建议的增强解析版本:

i: 1 ; add one as TextMate
parse text [any [thru newline (++ i)]]
print i

下面是我能想到的最好的简单非解析版本:

count-lines: function [text [string!]] [
    i: 1
    find-all text newline [++ i]
    i
]

它使用Rebol最新版本中的
函数
++
,以及R3或R2/Forward中的
查找所有
。您可以查看
find all
的源代码并内联您所查找和优化的内容,但像这样的情况正是我们编写的
find all
的目的,为什么不使用它呢?

为什么没有人提供最简单的解决方案我想知道:)

我不确定演出情况,但我认为演出速度相当快,因为蒂尔和芬奇都是本地人。 同时也可以使用

i: 1 while [t: find/tail t newline] [i: i + 1] i
== 4

只需要检查空字符串。如果它是一个函数,则参数系列需要有标题。

heheheread/行长度?temp是我认为关于read/lines->foreach lines temp的一件很棒的事情[count:count+1]

另一种方法是

temp: "line 1 ^M line2 ^M  line3 ^M "
length? parse temp newline ; that cuts the strings into a block 
;of multiple strings that represent each a line [ "line 1" "line2" "line3" ] 
:then you count how much  strings you have in the block with length? 
我喜欢用rebol编写代码,这很有趣

编辑我没有读整篇文章,所以我的解决方案已经以不同的方式提出了

好的,为了弥补我发布一个已经发布的解决方案的过失,我将对该解决方案的意外行为进行深入的评论。不计算多个链式回车(使用rebol3 linux…)


不是最有效的,但可能是最快的解决方案之一(无论如何,如果运行基准测试,我想看看这个解决方案的性能如何):



此外,任何测试速度/效率声明准确性的帮助都是受欢迎的。Rebol程序员会认为空字符串有一行。对零的恐惧是怎么回事-任何性能考虑因素中的另一件事是输入的性质。我会研究任何你尝试使用各种输入的技术…一些例子:空字符串,“所有换行的长字符串”,“零换行的长字符串”…字符串源不是一个文件,你的意思是为了读取/行而将字符串写入一个文件吗?好的,那么我们必须增加将值写入文件,然后再将其读回的开销。或者您可以尝试
deline/lines
,它在内存中执行相同的操作,而不需要文件。不过要小心:在R2中,
deline/lines
是夹层结构,所以它不会像其他一些解决方案那样快。在R3中,它是本地的。不管怎样,
read/lines
deline/lines
都会复制源字符串,因此我们必须看看这种开销是否超过了单个本机调用的优势。我可以确认这种方法是有效的,不过BriaH是一个真正的程序员,我只是提出了解决方案。如果您需要时间优化或其他高级技术解决方案,请与他联系。:-)我在这里担心的是,它创建的新字符串与行数一样多这是一个非常简洁的单行程序,但我担心它比问题中的
parse
示例做得更多。想听听其他的意见…这种方法的另一个缺点是使用
parse
作为
split
-在这种情况下它会中断:
parse{one^/“two^/three”}^/“
优点。我怀疑额外的记忆会是个问题。我作弊并且没有测试额外的gc:),但是引用处理是一个坏的惊喜。有趣的方法(任何人)有什么想法吗?有趣,Darius!顺便说一句,如果你愿意加入我们,我们有一个建议…任何修改方法对于长字符串都可能很慢,因为在修改过程中转换序列的开销。但是分析器会告诉我们哪种方法更好。我需要将其与上面的
”parse
函数进行对比,看看哪种方法更快。我还担心需要复制
,以免修改原始字符串从而影响效率。样式说明:函数返回换行数,而不是根据“文本编辑器原则”(参见问题)返回
s+1
。请注意,在Rebol 3中,REMOVE-EACH返回删除计数,不是修改后的系列。因此,您的建议可以是
删除REBOL3中的每个VS[v=#“^/”]
。(这实际上是我遇到的第一个用例,其中R3中相当奇怪的REMOVE-EACH返回值很有用。)这应该是
++I
?(没有注意到有一个
++
:)也可以启动
i:0
并说
[newline | end](++i)
。如果您想包含计数,请将初始化移到规则中,如下所示:
(i:1)
如果我可以将其行化:
计数行:func[text[string!]/i][parse text[(i:1)any through-newline(++i]i]
@HostileFork
/i
也会创建一个本地文件
/local
是一种没有正式意义的约定。虽然我会在正式环境中使用
/local
。Rebolek的
解析
解决方案可能更快,但只有探查器可以确定。另一个有趣的新功能,thks!
find all
函数是一个很好的选择,可以更改为本机。这里的
while
解决方案就是我所说的“inline
find all
并优化”注释。
until
解决方案可能更快,因为
until
更简单
count-lines: function [text [string!]] [
    i: 1
    find-all text newline [++ i]
    i
]
t: "abc^/de^/f^/ghi"
i: 0 until [i: i + 1 not t: find/tail t newline] i
== 4
i: 1 while [t: find/tail t newline] [i: i + 1] i
== 4
temp: "line 1 ^M line2 ^M  line3 ^M "
length? parse temp newline ; that cuts the strings into a block 
;of multiple strings that represent each a line [ "line 1" "line2" "line3" ] 
:then you count how much  strings you have in the block with length? 
>> a: "line1 ^M line2 ^M line3 ^M^M"
== "line1 ^M line2 ^M line3 ^M^M"

>> length? parse a newline 
== 3
>> s: "1^/2^/ ^/^/3"
>> (length? s) - length? trim/with copy s newline
== 4