Q:
在我以前使用asp.net的工作中,我考虑的只是如何完成所需的任务,不在乎 性能,但是现在我想,在我已经学到很多有关如何在某种程度上处理这种语言的知识之后,我应该专注于性能问题。 当我学习概念和语法时,我的老师告诉我,性能问题并不那么重要,首要任务始终是及时完成所需的任务,而且硬件和网络基础设施的革命将为你解决代码中的巨大性能差距! 现在,我想了解有关asp.net和整个Web中性能问题的一些技巧。如果有关于此想法的示例,我将不胜感激。 例如,有人告诉我:int count = dataTable.Rows.Count;
for(int i = 0 ; i<count ; i++)
{
//Do some thing
}
比这个更重要:
for(int i = 0 ; i<dataTable.Rows.Count ; i++)
{
//Do some thing
}
The first version can be more performant, since you don't re-evaluate dataTable.Rows.Count
in every iteration of the loop.
If every call to dataTable.Rows.Count
is expensive (and potentially can be), the first version will indeed be better.
As for general tips about performance:
Simple: don't optimise prematurely. And in this case, that is premature. There is a subtle difference between hoisting the length vs querying it each time, but in any sane circumstance, a: we're talking nanoseconds if that, and b: it changes depending on whether it is a naked vector, a list, an ilist, etc. Don't learn a general rule.
But the bigger problem here: that specific example is absolutely irrelevant to overall performance; you are talking about a DataTable
; a DataTable
presumably that is being populated from a database, which is out-of-process, and probably on a different machine. You are comparing nanoseconds (possibly less) against network latency (normally 0.3ms on a local LAN) plus query time, plus bandwidth (depends on the query).
You can't change the speed of light (latency), but you can write an efficient data query that accesses only the required data, using appropriate indexing and possibly denormalization. Oh, and N+1 - that's a biggie.
Likewise, even in memory - most bottlenecks tend to be due to things like inappropriate looping (when a hash-based lookup would be better), or not using appropriate caching to remove the need to constantly query data over and over and over and over and over.
On "the web in general"; caching, compression (transport and content - for example js/css minification), cookie-free domains for static content (maybe a CDN), farms of servers, fat pipes, and proper CPUs...