Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Week of the year" Algorithm needs improvements
    primarykey
    data
    text
    <p>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. </p> <p>It's working but performance is lagging. It is the 3rd function running using Task.Factory.StartNew (I have also tried parallel.Invoke).</p> <p>Results of timing tests.</p> <p>before: 00:00:05.58</p> <p>after: 00:00:23.27</p> <p><strong>UPDATE</strong></p> <p>Added break after each trading week is set. Time improved but still slow.</p> <p>new time: 00:00:15.74</p> <p>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.</p> <pre><code>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&lt;TradingWeek&gt; tradingWeek = new List&lt;TradingWeek&gt;(); 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 &gt; pe.Date.DayOfYear &amp;&amp; LoopDate_Begin.DayOfYear &lt; (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()); } } </code></pre> <p>Please help.</p> <p><strong>UPDATE</strong></p> <p>NEW TIME</p> <p>00:00:06.686</p> <p>A vast improvement. Thanks all for your help.</p> <p>Revised code:</p> <pre><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(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload