“一周的一周”算法需要改进(“Week of the year” Algorithm needs improvements)
我有一个算法,可以扫描从.csv文件(约3700行)中读取的数据,并通过为当年的每个星期日运行count ++并将计数值指定为交易周来评估每个条目处于一年中的哪个交易周当日期在那个星期内。
它正在运作,但表现滞后。 这是使用Task.Factory.StartNew运行的第三个函数(我也尝试了parallel.Invoke)。
时间测试的结果。
之前:00:00:05.58
之后:00:00:23.27
UPDATE
每个交易周后设置添加休息。 时间改善但仍然缓慢。
新时间:00:00:15.74
就我们的目的而言,一年中的第一周是第一周(不是0),定义为从一年的第一天到周日。 如果一年的第一天是星期天,那么第一周的时间是1天。
private void SetDefiniteWeeks() { string FileLoc = FilePath + Market + ".csv"; string[] Data = File.ReadAllLines(FileLoc); var FileData = from D in Data let DataSplit = D.Split(',') select new { Date = DateTime.Parse(DataSplit[0]), ClosingPrice = double.Parse(DataSplit[4]) }; //assign each date to it's relevant week TradingWeek TW; List<TradingWeek> tradingWeek = new List<TradingWeek>(); foreach (var pe in FileData) { // DateTime dt = pe.Date; int Year = pe.Date.Year; string End_of_Week = "Sunday"; int WeekCount = 0; DateTime LoopDate_Begin = new DateTime(Year,1,1); DateTime LoopDate_End = new DateTime(Year,12,31); do { if (LoopDate_Begin.DayOfWeek.ToString() == End_of_Week) { WeekCount++; if (LoopDate_Begin.DayOfYear > pe.Date.DayOfYear && LoopDate_Begin.DayOfYear < (pe.Date.DayOfYear + 7)) { TW = new TradingWeek { Week = WeekCount, Date = pe.Date }; tradingWeek.Add(TW); break; } } LoopDate_Begin = LoopDate_Begin.AddDays(1); } while (LoopDate_Begin.Date.ToString() != LoopDate_End.Date.ToString()); } }
请帮忙。
UPDATE
新的时间
00:00:06.686
巨大的改进。 感谢你的帮助。
修改后的代码:
CalendarWeekRule cw = CalendarWeekRule.FirstDay; var calendar = CultureInfo.CurrentCulture.Calendar; var trad_Week = (from pe in FileData select new TradingWeek { Date = pe.Date, Week = (calendar.GetWeekOfYear(pe.Date, cw,DayOfWeek.Sunday)) }).ToList();
I have an algorithm which scans through data read from a .csv file(approx 3700 lines) and assess's which trading week of the year each entry is in by running a count++ for every Sunday of that year and assigning the count value as the trading week when the date falls within that week.
It's working but performance is lagging. It is the 3rd function running using Task.Factory.StartNew (I have also tried parallel.Invoke).
Results of timing tests.
before: 00:00:05.58
after: 00:00:23.27
UPDATE
Added break after each trading week is set. Time improved but still slow.
new time: 00:00:15.74
For our purposes the 1st week of the year is week 1(not 0) and is defined as from the first day of the year until the Sunday. If the first day of the year is a Sunday the length of week 1 is 1 day.
private void SetDefiniteWeeks() { string FileLoc = FilePath + Market + ".csv"; string[] Data = File.ReadAllLines(FileLoc); var FileData = from D in Data let DataSplit = D.Split(',') select new { Date = DateTime.Parse(DataSplit[0]), ClosingPrice = double.Parse(DataSplit[4]) }; //assign each date to it's relevant week TradingWeek TW; List<TradingWeek> tradingWeek = new List<TradingWeek>(); foreach (var pe in FileData) { // DateTime dt = pe.Date; int Year = pe.Date.Year; string End_of_Week = "Sunday"; int WeekCount = 0; DateTime LoopDate_Begin = new DateTime(Year,1,1); DateTime LoopDate_End = new DateTime(Year,12,31); do { if (LoopDate_Begin.DayOfWeek.ToString() == End_of_Week) { WeekCount++; if (LoopDate_Begin.DayOfYear > pe.Date.DayOfYear && LoopDate_Begin.DayOfYear < (pe.Date.DayOfYear + 7)) { TW = new TradingWeek { Week = WeekCount, Date = pe.Date }; tradingWeek.Add(TW); break; } } LoopDate_Begin = LoopDate_Begin.AddDays(1); } while (LoopDate_Begin.Date.ToString() != LoopDate_End.Date.ToString()); } }
Please help.
UPDATE
NEW TIME
00:00:06.686
A vast improvement. Thanks all for your help.
Revised code:
CalendarWeekRule cw = CalendarWeekRule.FirstDay; var calendar = CultureInfo.CurrentCulture.Calendar; var trad_Week = (from pe in FileData select new TradingWeek { Date = pe.Date, Week = (calendar.GetWeekOfYear(pe.Date, cw,DayOfWeek.Sunday)) }).ToList();
原文:https://stackoverflow.com/questions/3355137
最满意答案
代码中有一些语法错误! 这就是显示500的原因。
很可能你必须在
apache.conf
或php.ini
启用display_startup_errors=1
或者执行ini_set();
PS寻找更多
blahblah_error=0
并改为blahblash_error=1
,你就完成了。在做任何这些更改之后,不要忘记重新启动apache
The answer to my problem was a permissions issue.
I moved the file back to the core directory, and decided to forget trying to get permissions just right on every folder in between, and bam, worked like a charm.
Case in Point, when you are getting a 500 error from navigating directly to the actual url, permissions is a likely suspect.
Also, the headers I included were not needed.
相关问答
更多-
我会说,这个调用失败的原因是因为在IIS下使用Windows身份验证时Safari中存在一个错误。 转到您网站的身份验证设置。 右键单击Windows身份验证,选择提供程序并删除协商,从而使NTLM正常工作。 我没有测试过Kerberos。 此问题仅出现在Safari的某些版本中。 I would say that the reason the call is failing is because of a bug in Safari when working with Windows Authentica ...
-
代码中有一些语法错误! 这就是显示500的原因。 很可能你必须在apache.conf或php.ini启用display_startup_errors=1或者执行ini_set(); PS寻找更多blahblah_error=0并改为blahblash_error=1 ,你就完成了。 在做任何这些更改之后,不要忘记重新启动apache The answer to my problem was a permissions issue. I moved the file back to the core dir ...
-
发送ajax调用时出错[内部服务器错误500](Getting error while sending ajax call [internal server error 500])[2023-12-06]
您必须在web.config中将maxJsonLength属性调整为更高的值才能解决此问题。 -
500(内部服务器错误),同时使用ajax codeignitor(500 (Internal Server Error) while using ajax codeigniter)[2022-03-27]
用这个改变你的代码 $query = "SELECT * from info_user Where user_status ='1'"; if(!empty($size)){ $query .= " and city in('".$size."')"; } if(!empty($sprice) && !empty($eprice)){ $query .= " and charge_per_hour >='".$sprice."' an ... -
好的,问题是根据条带API,令牌只能使用一次,所以在我的情况下这是解决方案: 使用令牌一次创建一个返回唯一客户ID的新客户。 通过传递第一次调用返回的客户ID而不是令牌,进行第二次ajax调用(向客户收费)。 Okay so the issue was that according to the stripe API, the token can only be used once, so in my case this was the solution: Use the token once to cre ...
-
发生这种情况是因为您没有在模型中进行验证,因此当它尝试创建记录时,它会失败,但会出现异常。 为避免这种情况,您可以在模型中添加验证,这将检查列是否唯一; 如果验证成功,则将创建记录,否则它将返回false并创建错误消息(而不是引发异常)。 您可以在模型验证中添加uniqueness: true ,如下所示: class DistribuicaoPorCargo < ApplicationRecord # scopes, callbacks, ... validates :local_da_prov ...
-
从POST删除id Url.Id在Url中用于GET请求。但是如果要在ajax调用的数据部分中使用POST使用Json.stringify() 。 $.ajax({ url: "/Manufacturer/Delete", type: "POST", contentType: "application/json; charset=utf-8", headers: { 'RequestVerificationToken': ...
-
按照[教程]( https://www.youtube.com/watch?v=eoGITOPpBfU )得到了一个结果。 不确定最初的问题是什么。 Got a result by following this [tutorial] (https://www.youtube.com/watch?v=eoGITOPpBfU). Not sure what the initial problem was.
-
用这个 print json_encode(array('type'=>'3', 'data'=>$galleryGrid)); 代替 print json_encode(['type'=>'3', 'data'=>$galleryGrid]); Use this print json_encode(array('type'=>'3', 'data'=>$galleryGrid)); instead of print json_encode(['type'=>'3', 'data'=>$galle ...
-
检查确保您的.htaccess或更改您的网址,如下所示 url: baseurl + "index.php/admin/announcement/remove_attachment", I solved my issue by editing the controller function and not by calling the model. Now my controller is as follows function remove_attachment() { $id=$this->inpu ...