patterncsharpMinor
Faster Iteration Functions
Viewed 0 times
iterationfasterfunctions
Problem
I am trying to build a simple C#/.Net console application that runs three iterations (see code below). The process runs as intended, but because of how many iterations there are, it can run upwards of 15 to 20 minutes. I intend to run through a list of nodes that would require individual simulations. Therefore requiring iterations for each node. Maybe 50 nodes.
I am wondering if there is a better method to run these iterations. I have just started reviewing Parallel.For() functions, but have not tested. And I have not seen many examples of chained Parallel.For() functions.
I am thinking the code below requires better iteration coding and a bit of multi-threading to really see an improvement in speed. Any suggestions? Is there anything glaring that looks wrong?
Here is the code:
```
static void Sim()
{
OE oe = new OE(); // node-path functionality
string ftC = "x"; // first call
double fC = 4; // fourth call
double tTime = 252; // days remaining
double calendar = 365; // full calendar days
double rate = .0065; /// interest rate
double sC = 1; // secondary call
double tC = 3.897; // third call
string path = @"c:\temp\output.csv"; // export
//tC settings
double i;
double nIncrement = .05;
double nMin = 2;
double nMax = 5;
// sC settings
double vi;
double nvIncrement = .005;
double nvMin = .10;
double nvMax = .50;
// time settings
double ti;
double ntIncrement = 5;
double ntMin = 1;
double ntMax = tTime + 4;
//double ntMax = 30;
// begin basic increments
// where I think I can find improvement in code
for (i = (tC - nMin); i < nMax + (tC + nIncrement); i += nIncrement) // tC loop
{
double nP = Math.Round(i, 2);
for (vi = (sC - nvMin); vi < (sC + nvMax) + nvIncrement; vi += nvIncrement) // sCatility loop
{
double nV = Math.Round(vi, 4);
for (ti = ntMin; ti <= ntMax; ti += ntIncrement) // time loop
I am wondering if there is a better method to run these iterations. I have just started reviewing Parallel.For() functions, but have not tested. And I have not seen many examples of chained Parallel.For() functions.
I am thinking the code below requires better iteration coding and a bit of multi-threading to really see an improvement in speed. Any suggestions? Is there anything glaring that looks wrong?
Here is the code:
```
static void Sim()
{
OE oe = new OE(); // node-path functionality
string ftC = "x"; // first call
double fC = 4; // fourth call
double tTime = 252; // days remaining
double calendar = 365; // full calendar days
double rate = .0065; /// interest rate
double sC = 1; // secondary call
double tC = 3.897; // third call
string path = @"c:\temp\output.csv"; // export
//tC settings
double i;
double nIncrement = .05;
double nMin = 2;
double nMax = 5;
// sC settings
double vi;
double nvIncrement = .005;
double nvMin = .10;
double nvMax = .50;
// time settings
double ti;
double ntIncrement = 5;
double ntMin = 1;
double ntMax = tTime + 4;
//double ntMax = 30;
// begin basic increments
// where I think I can find improvement in code
for (i = (tC - nMin); i < nMax + (tC + nIncrement); i += nIncrement) // tC loop
{
double nP = Math.Round(i, 2);
for (vi = (sC - nvMin); vi < (sC + nvMax) + nvIncrement; vi += nvIncrement) // sCatility loop
{
double nV = Math.Round(vi, 4);
for (ti = ntMin; ti <= ntMax; ti += ntIncrement) // time loop
Solution
Since this is code review, I would like to point out that your variable naming scheme makes your code hard to read. Why abbreviate anything? Why not call
fC as just fourthCall? etc.Context
StackExchange Code Review Q#28672, answer score: 5
Revisions (0)
No revisions yet.