2nd Try - Why is my Time algorithm so slow (simpler version)

R

Roy Gourgi

Hi,

Here is a much simpler version of my time scheduling program. As you can see
in the GV (Global Variable class) I have 2 arrays gaSumWorkDays (already
initialized with the values) and gaSeconds. All I want to do when I run my
CVerifyMissingTime class is to verify whether the sum of the 2 colums of
gaSeconds in gaSumWorkDays is > 0.

I hope that this is more intuitive than the first version.



Here is the code below. I have indeted it to make it clearer.



TIA

Roy







using System;

class cMain

{

public static int Main()

{


int lnTotMissingTime=0;


int t = DateTime.Now.Minute*60+DateTime.Now.Second;

Console.WriteLine("TimeStarted "+t);

lnTotMissingTime=CVerifyMissingTime.VerifyMissingTime();


Console.WriteLine("Total No of Missing Time {0}
",lnTotMissingTime);

t = DateTime.Now.Minute*60+DateTime.Now.Second;

Console.WriteLine("Time Ended "+t);

Console.ReadLine();

return 0;

}

}



public class GV

{

public const int kNoWeeks=11;

public static int gnSumSeconds = 0;

public static int[] gaWorkDays= new int [7];

public static int[] gaSumWorkDays = new int[61]

{ 0
,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,0};



public static int[,] gaSeconds = new int [kNoWeeks+1,3]

{

{ 0, 0, 0},

{ 0, 9, 0},

{ 0, 17, 1},

{ 0, 24, 2},

{ 0, 30, 3},

{ 0, 35, 4},

{ 0, 39, 5},

{ 0, 42, 6},

{ 0, 44, 7},

{ 0, 45, 8},

{0, 0, 9},

{0, 0, 10}};

}





class CVerifyMissingTime

{

public static int VerifyMissingTime() {

int lnSumMissingTime=0;

for (int xxxLoop=1 ; xxxLoop<=10; xxxLoop++) {

lnSumMissingTime=0;

for (int lp1=1 ; lp1<=GV.kNoWeeks-5 ; lp1++) {

for (int lp2=lp1+1 ; lp2<=GV.kNoWeeks-4 ;
lp2++) {

for (int lp3=lp2+1 ;
lp3<=GV.kNoWeeks-3 ; lp3++) {

for (int lp4=lp3+1 ;
lp4<=GV.kNoWeeks-2 ; lp4++) {

for (int lp5=lp4+1 ;
lp5<=GV.kNoWeeks-1 ; lp5++) {

for (int
lp6=lp5+1 ; lp6<=GV.kNoWeeks ; lp6++) {



if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0) {


lp6=lp5=lp4=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0) {


lp6=lp5=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp3,2]]>0) {


lp6=lp5=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp3,2]]>0) {


lp6=lp5=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp3,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp4,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp3,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp4,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp3,1]+GV.gaSeconds[lp4,2]]>0) {


lp6=100;


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp3,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp4,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp5,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp3,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp4,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp2,1]+GV.gaSeconds[lp5,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp3,1]+GV.gaSeconds[lp4,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp3,1]+GV.gaSeconds[lp5,2]]>0) {


continue;

}

if
(GV.gaSumWorkDays[GV.gaSeconds[lp4,1]+GV.gaSeconds[lp5,2]]>0) {


continue;

}

lnSumMissingTime++;

}

}

}

}

}

}

}

Console.WriteLine("Missing Time {0}",lnSumMissingTime);

return lnSumMissingTime;

}

}
 
J

Jon Skeet [C# MVP]

Roy Gourgi said:
Here is a much simpler version of my time scheduling program. As you can see
in the GV (Global Variable class) I have 2 arrays gaSumWorkDays (already
initialized with the values) and gaSeconds. All I want to do when I run my
CVerifyMissingTime class is to verify whether the sum of the 2 colums of
gaSeconds in gaSumWorkDays is > 0.

I hope that this is more intuitive than the first version.

It's slightly simpler, but there's still no indication of what it's
actually meant to do.

I also suspect that there are some bugs in it. For instance, at the
start of the innermost loop (with that many loops, you should really be
considering recursion) you have:

if (GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0)
{
lp6=lp5=lp4=100;
continue;
}
if (GV.gaSumWorkDays[GV.gaSeconds[lp1,1]+GV.gaSeconds[lp2,2]]>0)
{
lp6=lp5=100;
continue;
}

Now, under what circumstances do you foresee the second "if" condition
being true?

Bugs are hard to find and even harder to fix in code which doesn't have
an obvious meaning.

A first step towards improving the readability here would be to write a
method which took four parameters and returned whether
GV.gaSumWorkDay[GV.gaSeconds[first,second]+GV.gaSeconds[third,fourth]]
was greater than 0 or not. Note that with such a method in place, it
would be *much* easier to then refactor your code to use jagged arrays
rather than rectangular arrays. You could also give the method a name
which indicated what it was actually meant to do...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top