在方法中初始化构造器,使用效率低问题

sunchaohuang 2008-09-11 09:29:07
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Demo
{
class Program
{
static void Main(string[] args)
{
const Int32 nummax = 1000*1000*1000;
//Demo.x = 123;将初始化代码添加到此处Test1与Test2执行效率相同
Test1(nummax);
Test2(nummax);

Console.Read();
}

static void Test1(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Demo.x = 123;//将初始化代码添加到此处执行效率并没有被优化
for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}
Console.WriteLine("Test1:{0} Test1",sw.Elapsed);
}

static void Test2(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();

for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}

Console.WriteLine("Test2:{0} Test2", sw.Elapsed);
}
}

internal sealed class Demo
{
public static Int32 x=5;
static Demo()
{
x = 123;
}
}
}


经过测试在不通配置下以上程序相差在 1.5秒-3倍之间

理想状态:将Demo.x=123放在for循环前,执行时间应该缩短,可放置在方法中没有效果,需要将Demo.x=123提取到方法的入口点处。
...全文
173 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
koyote_love 2008-09-17
  • 打赏
  • 举报
回复
友情帮顶!
zpcoder 2008-09-17
  • 打赏
  • 举报
回复
我把你的代码测试了多次,


static void Main(string[] args)
{
const Int32 nummax = 1000*1000*1000;
//Demo.x = 123;//A
Test1(nummax);
Test2(nummax);

Console.Read();
}

static void Test1(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Demo.x = 123;//B
for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}
Console.WriteLine("Test1:{0} Test1",sw.Elapsed);
}


应该是: 如果,把A,B 全部去掉.Test1比Text2 慢约5倍

全打开,或任意打开一个, Test1和Test2 相当.
wanghao3616 2008-09-17
  • 打赏
  • 举报
回复
调用 结构的第一个方法 的 效率低
我觉得 有可能是 初始化结构后的一段时间
后面系统在做一些和当前进程无关的事情
把 for循环的 效率 拉低了
zpcoder 2008-09-17
  • 打赏
  • 举报
回复
你的 Test1 里有两个 Demo.x=123; 倒底何意?
wanghao3616 2008-09-17
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Demo
{
class Program
{
static int j;
static void Main(string[] args)
{
const Int32 nummax = 1000 * 1000 * 1000;
//Demo.x = 123;//将初始化代码添加到此处Test1与Test2执行效率相同
//j = new int();
Console.WriteLine("2");
Test1(nummax);
Console.WriteLine("3");
Test2(nummax);
Console.WriteLine("4");

Console.Read();
}

static void Test1(int nummax)
{
Stopwatch sw = new Stopwatch();

//Demo.x = 123;//将初始化代码添加到此处执行效率并没有被优化
j = new int();
sw.Start();

for (int i = 0; i < nummax; i++)
{
j = 123;
//Demo.x = 123;
}
Console.WriteLine("Test1:{0} Test1", sw.Elapsed);
}

static void Test2(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();

for (int i = 0; i < nummax; i++)
{
j = 123;
//Demo.x = 123;
}

Console.WriteLine("Test2:{0} Test2", sw.Elapsed);
}
}

internal sealed class Demo
{
public static Int32 x = 5;
static Demo()
{
x = 123;
}
}
}

你看下我的和你的 有什么 不同吗 ?
我的是放到那里都相差不大
sunchaohuang 2008-09-12
  • 打赏
  • 举报
回复
这就是.NET机制问题 书上找不到答案 楼上的懂不
homesos 2008-09-12
  • 打赏
  • 举报
回复
搞的是什么?
搞这些有什么意义?

还不如多学习一些关于.NET机制的书籍
homesos 2008-09-12
  • 打赏
  • 举报
回复
噢 这就是机制啊
那就讲个所以然出来嘛
JeffChung 2008-09-12
  • 打赏
  • 举报
回复
关注
sunchaohuang 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wanghao3616 的回复:]
应该是 main函数 和 普通函数的机制的问题
[/Quote]

你测试过吗?如果可以将Demo构造器的优化在方法中实现给个列子吧
wanghao3616 2008-09-11
  • 打赏
  • 举报
回复
应该是 main函数 和 普通函数的机制的问题
wanghao3616 2008-09-11
  • 打赏
  • 举报
回复
关注ing
Crazy_Xia 2008-09-11
  • 打赏
  • 举报
回复
静态构造函数只会执行一次不知道楼住要怎么优化
正宗熊猫哥 2008-09-11
  • 打赏
  • 举报
回复
for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}

每次都是一样的赋值。搞个循环做什么?

还是我看不懂。。。。晕
Crazy_Xia 2008-09-11
  • 打赏
  • 举报
回复
x作为静态成员在类被加载到内存中时就被分配了内存空间
Demo.x写在哪个位置有影响么?
Crazy_Xia 2008-09-11
  • 打赏
  • 举报
回复
看不懂注释
Demo.x放在方法体外和方法体内哪一种执行数度快?
是否两种方式Test1和Test2执行速度都一样?
关注

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧