日期:2014-05-20  浏览次数:20973 次

导入名字空间会否对性能产生影响?
比如某个名字空间中包含大量的静态方法,会否对性能产生影响?
除了用户体验外,它与使用完全限定名有何区别?

------解决方案--------------------
全限定名代码
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.IO.FileStream fs = System.IO.File.Open(@ "c:\aa.txt ", System.IO.FileMode.OpenOrCreate);
for (int j = 0; j < 15; j++)
{
System.DateTime t1 = System.DateTime.Now;
System.Net.IPAddress ip = null;
for (int i = 0; i < 100000; i++)
{
ip = System.Net.IPAddress.Parse( "172.16.0.100 ");
}
Console.WriteLine((System.DateTime.Now - t1).ToString());
byte[] buf = System.Text.Encoding.Default.GetBytes((System.DateTime.Now - t1).ToString().ToCharArray());
fs.Seek(0, System.IO.SeekOrigin.End);
fs.Write(buf, 0, buf.Length);
fs.Write(System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()), 0, System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()).Length);
}
fs.Write(System.Text.Encoding.Default.GetBytes( "\r\n\r\n\r\n\r\n\r\n ".ToCharArray()), 0, System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()).Length);
fs.Close();
Console.Read();
}
}
}

导入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.IO.FileStream fs = System.IO.File.Open(@ "c:\aa.txt ", System.IO.FileMode.OpenOrCreate);
for (int j = 0; j < 15; j++)
{
System.DateTime t1 = System.DateTime.Now;
Net.IPAddress ip = null;
for (int i = 0; i < 100000; i++)
{
ip = Net.IPAddress.Parse( "172.16.0.100 ");
}
Console.WriteLine((System.DateTime.Now - t1).ToString());
byte[] buf = System.Text.Encoding.Default.GetBytes((System.DateTime.Now - t1).ToString().ToCharArray());
fs.Seek(0, System.IO.SeekOrigin.End);
fs.Write(buf, 0, buf.Length);
fs.Write(System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()), 0, System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()).Length);
}
fs.Write(System.Text.Encoding.Default.GetBytes( "\r\n\r\n\r\n\r\n\r\n ".ToCharArray()), 0, System.Text.Encoding.Default.GetBytes( "\r\n ".ToCharArray()).Length);
fs.Close();
Console.Read();
}
}
}


全限定名1 调式运行
00:00:02.8906250
00:00:02.7656250
00:00:02.7187500
00:00:02.8281250
00:00:02.7187500
00:00:02.8125000
00:00:02.6875000
00:00:02.8125000
00:00:02.7343750
00:00:02.8125000
00:00:02.8437500
00:00:02.9062500
00:00:02.6718750
00:00:02.8437500
00:00:02.7500000

全限定名2 直接运行 平均值 0.229167
00:00:00.2500000
00:00:00.2343750
00:00:00.2031250
00:00:00.2343750
00:00:00.2187500
00:00:00.2031250
00:00:00.2500000
00:00:00.2187500
00:00:00.2500000
00:00:00.2187500