c# - The more request means slower read from cached objects?
问题描述
Please do not judgme for the type of question. It might be about software algorithms and concepts. That is why I must write the link that it is already asked on server fault.
1- > the algorithm and concept of project .net web app.
Client's onblur event makes an ajax call-> UI -> functionUI() -> > APP <<
functionUI()
int limit=450;
public bool functionUI(){
for(int i=0;i< limit ; i++){
functionApp(i);
}
}
public static bool functionApp(int i){
foreach(HolidayEntity h in Global.holidayGlobals.Values /*CachedList*/){
if(h.Value == i){
return false;
}
return true;
}
}
2- The question The limit value from ui side if it is 2 or 20 times the response duration is good . But when it is 450 times. It takes 40 seconds to get a result in client side. I know the code is not well organised but the question is why the application side so slow when it is responsible to make more calculations.
Any clue will be so usefull. Thank you.
Servers are Server 2018 SP 1, .net Frame Work 4.0.30319.42000 It is only happened in production environment. In development side the application runs quite fast even the limit is 450.
解决方案
基本上这种类型的缓慢来自于犯了这个错误:
for (var i = 0; i<100; i++)
{
// do a network-roundtrip/remoting/io/etc
}
解决方案是将其更改为:
// do a network-roundtrip/remoting/io/etc
for (var i = 0; i<100; i++)
{
// do something with the result of the above call
}
因此,如果我对您的理解正确,则数据Global.holidayGlobals.Values
来自另一个进程,例如内存缓存。如果是这样,您最简单的选择是将代码更改为:
int limit=450;
public bool functionUI()
{
var holidays = Global.holidayGlobals.Values;
for(int i=0;i< limit ; i++)
{
functionApp(i, holidays);
}
}
public static bool functionApp(int i, IEnumerable<HolidayEntity> holidays)
{
foreach(var h in holidays)
{
if(h.Value == i)
{
return false;
}
}
return true;
}
我们仍然在循环中做太多的工作(我们可以通过创建字典并查找它来改进算法)但是对于您的号码(450),与往返内存缓存的时间相比,这可以忽略不计。
更新
从评论到问题,如果holidays
是Dictionary<int, HolidayEntity>
这样,代码可以像这样变得更好:
public bool functionUI()
{
var holidays = Global.holidayGlobals.Values;
for(int i=0;i< limit ; i++)
{
functionApp(i, holidays);
}
}
public static bool functionApp(int i, IEnumerable<HolidayEntity> holidays)
{
if (holidays.TryGetValue(i, out var h))
{
return false;
}
return true;
}
推荐阅读
- pandas - RDD vs Pandas Dataframe vs Direct Read 创建 Spark DataFrame
- javascript - 如何正确将照片添加到 Vue Carousel 3D 滑块?
- asp.net - 将 asp.net Core REACT 应用程序部署到 AWS Beanstalk 时出现 IdentityServer 错误
- php - 如何将拖动动作保存到数据库中
- memory - 如何解决 IAR 中的“链接器部分放置错误”
- javascript - 如何像 React.js 一样在 Java 中进行实时值更改
- python - 关闭在 python 的新窗口中打开的子进程
- python - 我收到 pyinstaller 的错误,从 py 到 exe 的转换工作正常,但是当我尝试打开它时出现错误
- css - :not() 用于 httml[lang] 的伪类不起作用
- android - Flutter 从 firebase 填充 DropDown 中的数据