将实习串有助于解析器的表现? [英] Will Interning strings help performance in a parser?

查看:95
本文介绍了将实习串有助于解析器的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你正在解析,让我们只说HTML,一旦你读的元素名称,不会是来实习是有益?这里的逻辑是,这个解析器会一遍又一遍解析相同的字符串(元素名称)? 。和几个文件将被解析

If you are parsing, lets just say HTML, once you read the element name, will it be beneficial to intern it? The logic here is that this parser will parse the same strings (element names) over and over again? And several documents will be parsed.

理论:

// elemName is checked for null.
MarkupNode node = new MarkupNode() 
{
   Name = String.IsInterned(elemName) ? elemName : String.Intern(elemName),
   ...
};

这问题是对这个问题的串实习内存

This question was motivated by the question string-interning-memory.

推荐答案

我真的不能确切地说这是否会帮助你的表现与否。这将取决于你用多少字符串,以及如何频繁地创建这些字符串的实例。实习一般是自动完成的,因此,明确检查,如果字符串被拘禁可能会增加你的开销,降低性能。当涉及到内存使用情况,实习弦绝对可以使用较少的内存。

I couldn't really say exactly whether this would help your performance or not. It would depend on how many strings you use, and how frequently you create instances of those strings. Interning is generally done automatically, so explicitly checking if the string is interned may actually increase your overhead and reduce your performance. When it comes to memory usage, interned strings can definitely use less memory.

如果您确实希望使用字符串实习,也有一些更好的方法来实现它。首先,我会在一个静态类全面公开字符串常量坚持你的元素名称。在你的程序源代码中发现的字面任何字符串肯定,并自动扣留。当你的应用程序加载这样的字符串被装入实习生池。如果你的字符串不能被定义为编译时实习生的准备常数,那么我会简单地调用的中的String.intern(...)的而不是拼尽了全力三元表达的 String.IsInterned( ...)? ...:中的String.intern(...)的。实习生方法会自动检查字符串实习,返回实习版本,如果是这样,否则将字符串添加到实习生池,并返回,如果事实并非如此。无需手动检查IsInterned自己。

If you do wish to use string interning, there are some better ways to achieve it. First and foremost, I would stick your element names in a static class full of public string constants. Any string literal found in your program source code is definitely and automatically interned. Such strings are loaded into the intern pool when your application is loaded. If your strings can not be defined as constants for compile-time intern preparation, then I would simply call String.Intern(...) rather than doing the full ternary expression String.IsInterned(...) ? ... : String.Intern(...). The Intern method will automatically check if the string is interned, return the interned version if it is, and will otherwise add the string to the intern pool and return that if it is not. No need to manually check IsInterned yourself.

同样,我也不能说手动实习字符串是否会提高性能。如果使用常量,它们将自动为实习你,在最优化的方式,那就是提高定期重复使用的字符串性能和内存使用的最佳方法。我就老老实实建议你远离手动实习了,并让你的编译器和运行手柄优化。

Again, I can not say whether manually interning strings will improve performance. If you use constants, they will be automatically interned for you, in the most optimal way, and that is the best approach to improving performance and memory usage of regularly reused strings. I would honestly recommend you stay away from manual interning, and let the compiler and runtime handle optimization for you.

这篇关于将实习串有助于解析器的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆