小编die*_*be.的帖子

为什么是str = str.Replace().Replace(); 快于str = str.Replace(); str = str.Replace()?

我正在做一个本地测试来比较C#中的String和StringBuilder的Replace操作性能,但对于String,我使用了以下代码:

String str = "String to be tested. String to be tested. String to be tested."
str = str.Replace("i", "in");
str = str.Replace("to", "ott");
str = str.Replace("St", "Tsr");
str = str.Replace(".", "\n");
str = str.Replace("be", "or be");
str = str.Replace("al", "xd");
Run Code Online (Sandbox Code Playgroud)

但是,在注意到String.Replace()比StringBuilder.Replace()更快之后,我继续针对上面的代码测试以下代码:

String str = "String to be tested. String to be tested. String to be tested."
str = str.Replace("i", "in").Replace("to", "ott").Replace("St", "Tsr").Replace(".", "\n").Replace("be", "or be").Replace("al", "xd");
Run Code Online (Sandbox Code Playgroud)

最后一个结果是快了大约10%到15%,任何想法为何更快?是否将值分配给昂贵的同一个变量?

c# string replace inline assign

4
推荐指数
2
解决办法
198
查看次数

如何从python PLY中的方案解释“do”循环

我正在使用 PLY (Python Lex-Yacc) 为 Scheme 做一个解释器,但我无法使用堆栈中的值来实现“do”循环,该值跟踪与变量对应的 ID(例如 i 用于环形)。

这是一个编译器设计课程的最终项目,主要问题是向堆栈添加值的那一刻,我使用字典将变量名作为键,然后是值,但它不是在它应该分配的那一刻,它尝试在一个值和变量之间进行比较,但它失败了,因为堆栈仍然是空的。

这是代码中最重要的部分:

ids = { }

def p_program(p):
    'program : form'
    #return p
    print(p[1])

def p_form_a(p):
    '''
    form : definition
         | expression
    '''
    p[0] = p[1]

def p_expression(p):
    '''
    expression : constant
               | do_expression
               | ID
               | display
    '''
    p[0] = p[1]

def p_do_expression_a(p):
    #       0           1      2    3      4     5         6      7    8      9         10        11              12              13        14
    'do_expression : OPEN_PAR DO OPEN_PAR ID constant OPEN_PAR symbol ID …
Run Code Online (Sandbox Code Playgroud)

python scheme interpreter loops ply

1
推荐指数
1
解决办法
531
查看次数

标签 统计

assign ×1

c# ×1

inline ×1

interpreter ×1

loops ×1

ply ×1

python ×1

replace ×1

scheme ×1

string ×1