函数内的常量字符串
来自Mricrosoft XNA Education Catalog 的片段:
/// <summary>
/// Draws the control, using SpriteBatch and SpriteFont.
/// </summary>
protected override void Draw()
{
const string message = "Hello, World!\n" +
"\n" +
"I'm an XNA Framework GraphicsDevice,\n" +
"running inside a WinForms application.\n" +
"\n" +
"This text is drawn using SpriteBatch,\n" +
"with a SpriteFont that was loaded\n" +
"through the ContentManager.\n" +
"\n" +
"The pane to my right contains a\n" +
"spinning 3D triangle.";
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
spriteBatch.End();
}
每秒调用Draw 60 次。在绘图内分配消息是否有任何性能开销?这和我将它移动到静态帮助器类一样吗?据我记得,成本表达式是由 C# 编译器计算的。这里const修饰符改变了什么?
A snippet from Mricrosoft XNA Education Catalog:
/// <summary>
/// Draws the control, using SpriteBatch and SpriteFont.
/// </summary>
protected override void Draw()
{
const string message = "Hello, World!\n" +
"\n" +
"I'm an XNA Framework GraphicsDevice,\n" +
"running inside a WinForms application.\n" +
"\n" +
"This text is drawn using SpriteBatch,\n" +
"with a SpriteFont that was loaded\n" +
"through the ContentManager.\n" +
"\n" +
"The pane to my right contains a\n" +
"spinning 3D triangle.";
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
spriteBatch.End();
}
Draw is called 60 times per second. Is there any performance overhead with assigning message inside the draw? Is it the same as if I move it to static helper class? As far I recall, cost expression is evaluated by C# compiler. What const modifier change here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
const 仅被评估一次。将其移至静态变量中不会获得任何好处。
A const is evaluated once only. You gain nothing by moving it away into a static variable.
不,它全部为您优化。
Nope its all optimized for you.