C# 反射使用缓存性能更差?很奇怪!

NqIceCoffee 2008-12-18 02:48:57
public static class ModelBuilder
{
private static Dictionary<string, PropertyInfo> PropertyDictionary = new Dictionary<string, PropertyInfo>();

public static TModel Build<TModel>(NameValueCollection inputCollection, string namePrefix) where TModel : new()
{
TModel model = new TModel();
Type modelType = model.GetType();
foreach (string elName in inputCollection.AllKeys)
{
if (!elName.StartsWith(namePrefix)) continue;

string value = inputCollection[elName];
if (string.IsNullOrEmpty(value)) continue;

string propName = elName;
if (!string.IsNullOrEmpty(namePrefix))
propName = elName.Substring(namePrefix.Length);

//PropertyInfo prop = modelType.GetProperty(propName); //不使用缓存的
PropertyInfo prop = GetPropertyInfo(modelType, propName); //使用缓存的
if (prop == null) continue;

bool flag = false;
object objValue = null;
Type propType = prop.PropertyType;

try
{
if (propType.IsEnum)
objValue = Enum.Parse(propType, value, true);
else
objValue = Convert.ChangeType(value, propType);

flag = true;
}
catch { }

if (flag)
prop.SetValue(model, objValue, null);
}
return model;
}

private static PropertyInfo GetPropertyInfo(Type modelType, string propertyName)
{
PropertyInfo property = null;
string propertyFullName = string.Format("{0}.{1}", modelType.FullName, propertyName);

if (PropertyDictionary.TryGetValue(propertyFullName, out property))
return property;

property = modelType.GetProperty(propertyName);
if (property != null)
{
lock (PropertyDictionary)
{
PropertyDictionary.Add(propertyFullName, property);
}
}
return property;
}
}


如上面所示,两种情况,使用缓存居然比不使用缓存的性能还差

随后附上测试代码
...全文
685 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
monom 2009-06-23
  • 打赏
  • 举报
回复
...
虽然这帖子是去年的,很早的了,但还是要回个贴,
希望以后看到这贴的人有收获。
monom 2009-06-23
  • 打赏
  • 举报
回复
property = modelType.GetProperty(propertyName);
是这句代码的问题,太影响性能了。

应该只做一次行读取所有属性,properties = modelType.GetProperties();
这样之后再开启缓存。


NqIceCoffee 2008-12-19
  • 打赏
  • 举报
回复
我顶
NqIceCoffee 2008-12-18
  • 打赏
  • 举报
回复
沉的很快。。。
wuyq11 2008-12-18
  • 打赏
  • 举报
回复
有时间也看看。UP
erik0930 2008-12-18
  • 打赏
  • 举报
回复
mark
NqIceCoffee 2008-12-18
  • 打赏
  • 举报
回复
连个JF的都没。。。。。。。
NqIceCoffee 2008-12-18
  • 打赏
  • 举报
回复
实体对象
    public class TestModel
{
public string Name { get; set; }
public int Count { get; set; }
public bool IsLimit { get; set; }
public DateTime Time { get; set; }
public ModelType Type { get; set; }
}

public enum ModelType
{
Table = 1,
View = 2
}


测试代码:
if (Request.HttpMethod == "POST")
{
DateTime sTime = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
TestModel test = ModelBuilder.Build<TestModel>(Request.Form, "txt");
}

TimeSpan ts = DateTime.Now - sTime;

Response.Write(ts.TotalMilliseconds.ToString());
}


页面HTML:
<form method="post">
<input type="text" name="txtName" />
<input type="text" name="txtCount" />
<input type="text" name="txtIsLimit" />
<input type="text" name="txtTime" />
<input type="text" name="txtType" />
<input type=submit input="提交" />
</form>



10W次累计结果:使用缓存,大概7850毫秒

不使用缓存7260毫秒

多次测试,均是这般,奇怪啊!!!

62,242

社区成员

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

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

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

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