HELP!! on queues..... 
Author Message
 HELP!! on queues.....

Thanks for the advice Ken.

In reguards to the other students, the student who had the program run
in 4 seconds had really weird results, however the 2 girls that had
their program finished in 30 seconds to a minute had almost identical
results to me. As a reminder my program finishes in 20 minutes!!!

The cust.inp file has 250 records. It is apparently real records from a
retail store here in Melbourne (although the original file has 10,000+
records).

Now my .h file is as follows:

/* Written by Petar Vucic *****FINAL ASSIGNMENT */

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define MAX 450

//      Define structure for the header file                            

typedef enum {false,true} boolean ;

typedef struct
{
                int      hh, mm, ss ;

Quote:
} timerec ;

typedef struct
{
                timerec sert, arrt ;

Quote:
} cust ;

typedef cust qtype ;
typedef struct
{
           qtype    data[MAX] ;
           int      front, rear ;

Quote:
} queue ;

//      Customer queue functions                                        

void create(queue *) ;
qtype first(queue) ;
void insert(queue *, qtype) ;
void deleteq(queue *) ;
boolean empty(queue) ;
boolean full(queue) ;

void create(queue * q)
{
        q->front = q->rear = 0 ;

Quote:
}

boolean empty(queue q)
{
        return(q.front == q.rear) ;

Quote:
}

boolean full(queue q)
{
        return(((q.front+1)%MAX) == q.rear) ;

Quote:
}

qtype first(queue q)
{
        return(q.data[q.rear]) ;

Quote:
}

void deleteq(queue * q)
{
        q->rear = (q->rear + 1) % MAX ;

Quote:
}

void insert(queue * q, qtype indata)
{
        q->data[q->front] = indata ;
        q->front = (q->front + 1) % MAX ;

Quote:
}

//      Customer time functions                                        

void writetime( timerec t )
{
          printf("%4d:%2d:%2d",t.hh,t.mm,t.ss) ;

Quote:
} ;

void set_time( timerec * t, int h, int m, int s)
{
          t->hh = h ;
          t->mm = m ;
          t->ss = s ;

Quote:
} ;

void fixtime( timerec * t )
{
          if (t->ss > 59)
          {
                  t->mm++ ;
                  t->ss = t->ss - 60 ;
          }
          if (t->mm > 59)
          {
                  t->hh++ ;
                  t->mm = t->mm - 60 ;
          }

Quote:
} ;

void updatetime( timerec *t )
{
          t->ss++ ;
          fixtime(t) ;

Quote:
} ;

void addtime( timerec * t1, timerec t2 )
{
          t1->ss = t1->ss + t2.ss ;
          fixtime(t1) ;
          t1->mm = t1->mm + t2.mm ;
          fixtime(t1) ;
          t1->hh = t1->hh + t2.hh ;

Quote:
}

void difftime( timerec t1, timerec t2, timerec * t3)
{
          if (t2.ss > t1.ss)
          {
             t3->ss = t1.ss + 60 - t2.ss ;
             t1.mm-- ;
          }
          else
             t3->ss = t1.ss - t2.ss ;

          if (t2.mm > t1.mm)
          {
             t3->mm = t1.mm + 60 - t2.mm ;
             t1.hh-- ;
          }
          else
             t3->mm = t1.mm - t2.mm ;

          t3->hh = t1.hh - t2.hh ;

Quote:
}

boolean comptime( timerec t1, timerec t2)
{
          boolean earlier = false;

          if (t1.hh < t2.hh)
             earlier = true ;
          if ((t1.hh == t2.hh) && (t1.mm < t2.mm))
             earlier = true ;
          if ((t1.hh == t2.hh) && (t1.mm == t2.mm) && (t1.ss <= t2.ss))
             earlier = true ;
          return(earlier) ;

Quote:
}

void getavg( timerec t, int n, timerec *at)
{
     float avgtime ;

     avgtime  = t.hh * (3600 / n) ;
     avgtime += t.mm * 60 / n ;
     avgtime += t.ss ;
     (*at).hh = avgtime / 3600 ;
     (*at).mm = (avgtime - (*at).hh*3600) / 60 ;
     (*at).ss = avgtime - (*at).hh*3600 - (*at).mm*60 ;

Quote:
}

I really feel that my .h file is not the reason why the program runs so
slow, and it is nothing to do with processor power etc.

Now once again the program is as follows:

#include "a:\custq.h"

void main()
{
          queue   q ;
          cust    a ;
          FILE    *inf ;
          char    line[80] ;
          int     i, nocust = 0, currcust=0, currserv=0, xloc, yloc ;
          timerec currtime, currwait, fivesec ;
          timerec avgwait, avgserv, totalwait, totalserv ;

          //clrscr() ;
          inf = fopen("cust.inp","r") ;
          create(&q) ;
          while (fgets(line,80,inf) != NULL)
          {
                  sscanf(line,"%d%d:%d:%d%d:%d:%d",&i,&a.arrt.hh,&a.arrt.mm,
                                        &a.arrt.ss,&a.sert.hh,&a.sert.mm,&a.sert.ss) ;
                  insert(&q,a) ;
                  nocust++ ;
          } ;

          set_time(&currtime,6,59,30) ;
          set_time(&totalserv,0,0,0) ;
          set_time(&totalwait,0,0,0) ;
          set_time(&fivesec,0,0,5) ;
          //window(1,1,80,25) ;
          while (!empty(q))
          {
                //  clrscr() ;
                  xloc = 15 ;
                  yloc = 10 ;
                  //gotoxy(xloc,yloc) ;
                  printf("Current time ") ;
                  //gotoxy(xloc+35,yloc++) ;
                  writetime(currtime) ;
                  a = first(q) ;
                  //gotoxy(xloc,yloc) ;
                  printf("Processing customer") ;
                  //gotoxy(xloc+42,yloc++) ;
                  printf("%3d",currcust) ;
                  //gotoxy(xloc,yloc) ;
                  printf("Next customer arrival time ") ;
                  //gotoxy(xloc+35,yloc++) ;
                  writetime(a.arrt) ;
                  if ((currserv <= 0) && comptime(a.arrt,currtime))
                  {
                     difftime(currtime,a.arrt,&currwait) ;
                     addtime(&totalwait,currwait) ;
                     deleteq(&q) ;
                     currserv = a.sert.mm*60 + a.sert.ss ;
                     currcust++ ;
                  }
                  //gotoxy(xloc,yloc) ;
                  printf("Serving time remaining") ;
                  //gotoxy(xloc+40,yloc++) ;
                  printf("%3d s",currserv) ;
                  //gotoxy(xloc,yloc) ;
                  printf("Accumulated total waiting time ") ;
                  //gotoxy(xloc+35,yloc++) ;
                  writetime(totalwait) ;
                  //gotoxy(xloc,yloc) ;
                  printf("Accumulated total serving time ") ;
                  //gotoxy(xloc+35,yloc) ;
                  writetime(totalserv) ;
                  if (currserv>0)
                  {
                        currserv-- ;
                        updatetime(&totalserv) ;
                        updatetime(&currtime) ;
                  }
                  else
                     set_time(&currtime,a.arrt.hh,a.arrt.mm,a.arrt.ss) ;
                //  gotoxy(xloc,yloc+10) ;
                 // delay(250) ;
          }
          yloc += 3 ;
          //gotoxy(xloc,yloc++) ;
          puts("Daily Customer Queue Analysis report") ;
          //gotoxy(xloc,yloc++) ;
          puts("====================================") ;
          //gotoxy(xloc,yloc++) ;
          printf("Total number of customers %d",nocust) ;
          getavg(totalserv,nocust,&avgserv) ;
          //gotoxy(xloc,yloc++) ;
          printf("Average serving time is ") ;
          writetime(avgserv) ;
          getavg(totalwait,nocust,&avgwait) ;
          //gotoxy(xloc,yloc) ;
          printf("Average waiting time is ") ;
          writetime(avgwait) ;

Quote:
}

(Please note that I have remarked out the goto, clear screen etc as I
was just trying something out...).

OK thats everything. I think I will try 2 queues and see if it speeds
things up, also I'll take your advice Ken and drop the fivesec all
together..

What puzzles me is that according to the results of my program they are
perfect, however I am stumped as to why it runs so S L O W....

Any further advice would be great!!!

Anyway, thanks for the reply and hope to hear from someone soon!!!

Petar Vucic.

PS I will include the cust.inp file as an attachment, is that ok on a
newsgroup? (Don't kill me...) :)

[ Cust.inp 7K ]
  1      7: 0: 0      0: 2:23
  2      7: 6: 6      0: 1:47
  3      7: 8:31      0: 3:44
  4      7:14:47      0: 2:16
  5      7:20:57      0: 1: 1
  6      7:23:16      0: 1:39
  7      7:28:33      0: 3:14
  8      7:34:42      0: 1:51
  9      7:37:55      0: 2:11
 10      7:44: 9      0: 1:31
 11      7:50:29      0: 3:10
 12      7:51:33      0: 3:36
 13      7:56:46      0: 2:13
 14      8: 0: 9      0: 2:41
 15      8: 1:32      0: 3:19
 16      8: 2: 0      0: 3:56
 17      8: 6:19      0: 2: 2
 18      8:11:23      0: 1:28
 19      8:12:50      0: 2: 4
 20      8:15: 6      0: 2: 7
 21      8:19:17      0: 3:24
 22      8:21:18      0: 1: 7
 23      8:25:43      0: 2:56
 24      8:31:50      0: 3: 3
 25      8:35:57      0: 1: 2
 26      8:41: 7      0: 1:58
 27      8:43:15      0: 3:10
 28      8:46:34      0: 2:36
 29      8:52:59      0: 2:56
 30      8:53:24      0: 2:39
 31      8:58:44      0: 1:16
 32      8:59:51      0: 3:21
 33      9: 4:10      0: 3: 4
 34      9: 5:12      0: 1:40
 35      9: 5:20      0: 2:47
 36      9: 6:29      0: 1:25
 37      9: 6:40      0: 2:14
 38      9: 8: 9      0: 2:31
 39      9: 9:25      0: 2:21
 40      9:10:25      0: 3:58
 41      9:10:27      0: 1:36
 42      9:12:33      0: 3:52
 43      9:12:36      0: 2:44
 44      9:15:41      0: 1:20
 45      9:19:47      0: 2: 2
 46      9:20: 8      0: 2:55
 47      9:21:35      0: 3:57
 48      9:21:47      0: 3:30
 49      9:24:55      0: 1:50
 50      9:28:58      0: 1:28
 51      9:34:22      0: 3:41
 52      9:35:50      0: 3:54
 53      9:40: 5      0: 1:20
 54      9:42: 8      0: 2: 9
 55      9:44: 8      0: 1:12
 56      9:45:31      0: 2:16
 57      9:50:48      0: 1:38
 58      9:53:57      0: 1:45
 59      9:56:16      0: 1: 6
 60      9:57:44      0: 1:39
 61      9:58:56      0: 1:43
 62      9:59:16      0: 2:47
 63     10: 0:43      0: 3:32
 64     10: 4:48      0: 1:10
 65     10:10:11      0: 3:15
 66     10:10:22      0: 1:19
 67     10:14:40      0: 1:26
 68     10:14:45      0: 3:51
 69     10:16: 2      0: 1:33
 70     10:17:22      0: 3:23
 71     10:18:34      0: 1:24
 72     10:20:56      0: 3:42
 73     10:21:14      0: 3:54
 74     10:25:38      0: 3:25
 75     10:29:51      0: 1:47
 76     10:34:13      0: 3:14
 77     10:39:19      0: 2: 0
 78     10:42:48      0: 2:31
 79     10:45: 1      0: 2:47
 80     10:45:11      0: 1:29
 81     10:47:25      0: 3:15
 82     10:49:35      0: 2:41
 83     10:54:43      0: 2:43
 84     10:54:53      0: 1:19
 85     10:55: 8      0: 2:17
 86     10:56: 9      0: 1:55
 87     10:56:34      0: 2: 2
 88     10:59: 0      0: 3:47
 89     11: 4: 3      0: 3:50
 90     11: 4: 8      0: 3:33
 91     11: 5:32      0: 2:39
 92     11: 8:59      0: 2:19
 93     11:11:18      0: 1:11
 94     11:12:18      0: 1:41
 95     11:14:22      0: 2:29
 96     11:15:48      0: 2:39
 97     11:18:15      0: 3:23
 98     11:18:40      0: 3: 6
 99     11:20:59      0: 3:31
100     11:22:27      0: 3:58
101     11:24:37      0: 1:16
102     11:27:43      0: 2:16
103     11:31: 3      0: 2:20
104     11:33:21      0: 2: 7
105     11:36:42      0: 3:49
106     11:38: 8      0: 3: 4
107     11:41:23      0: 2:51
108     11:44:40      0: 1:44
109     11:44:48      0: 1:26
110     11:45:15      0: 3:43
111     11:47:29      0: 2:30
112     11:50:38      0: 3:41
113     11:53: 1      0: 2:14
114     11:53:18      0: 3:43
115     11:54:33      0: 3:26
116     11:55:49      0: 2:17
117     11:57:56      0: 3:34
118     11:58: 4      0: 2:43
119     11:59:28      0: 2:33
120     12: 0:29      0: 1:22
121     12: 1:44      0: 2: 8
122     12: 4: 1      0: 3:14
123     12: 5:16      0: 1:22
124     12: 5:25      0: 2: 4
125     12: 5:36      0: 3:56
126     12: 6:44      0: 3: 4
127     12: 8:54      0: 3:31
128     12:12: 6      0: 3:54
129     12:15:29      0: 1:12
130     12:16:47      0: 3:11
131     12:18: 3      0: 2:49
132     12:21:15      0: 1:30
133     12:22:29      0: 2:32
134     12:23:42      0: 2:10
135     12:24:52      0: 1:17
136     12:26: 8      0: 3: 1
137     12:26:21      0: 3:33
138     12:29:24      0: 2:40
139     12:32:53      0: 3:44
140     12:35:22      0: 1:32
141     12:36:30      0: 1:58
142     12:36:44      0: 2:16
143     12:41:12      0: 2:31
144     12:44:24      0: 3:26
145     12:47:33      0: 1:45
146     12:51:45      0: 2:58
147     12:51:55      0: 3:22
148     12:53: 6      0: 2:59
149     12:54:21      0: 1:49
150     12:58:40      0: 2:32
151     12:59: 9      0: 3:41
152     13: 3:21      0: 1: 6
153     13: 6:32      0: 2:30
154     13: 9:41      0: 2:29
155     13:13:57      0: 2:17
156     13:18:11      0: 1:50
157     13:21:40      0: 2:51
158     13:24: 8      0: 3:56
159     13:27: 8      0: 1:18
160     13:30:16      0: 2:14
161     13:30:44      0: 1:25
162     13:33:56      0: 2:24
163     13:38:15      0: 3:51
164     13:39:26      0: 3:24
165     13:41:52      0: 3:26
166     13:46:17      0: 1: 5
167     13:50:30      0: 3:11
168     13:53:51      0: 1:59
169     13:57:19      0: 1: 6
170     13:57:40      0: 3:36
171     13:59:42      0: 1:57
172     14: 4: 7      0: 3:48
173     14: 4:24      0: 1:11
174     14: 8:45      0: 2:26
175     14:10:12      0: 1:28
176     14:11:15      0: 1:32
177     14:11:38      0: 2:33
178     14:12:43      0: 1:34
179     14:13:10      0: 3:19
180     14:13:28      0: 3:39
181     14:15:36      0: 2:53
182     14:19:56      0: 1:21
183     14:24:18      0: 1:49
184     14:25:18      0: 3: 4
185     14:25:43      0: 1:10
186     14:26:57      0: 3:36
187     14:27:58      0: 2:57
188     14:30: 4      0: 3:33
189     14:31:13      0: 1:58
190     14:37:40      0: 3:16
191     14:42:58      0: 2:14
192     14:47: 0      0: 1:42
193     14:50:16      0: 3:32
194     14:54:27      0: 2:54
195     14:59:32      0: 2:55
196     15: 2:53      0: 1:18
197     15: 7: 7      0: 1:48
198     15:12:18      0: 2: 1
199     15:14:45      0: 1:26
200     15:20:48      0: 3:35
201     15:20:58      0: 1:56
202     15:21: 9      0: 3:46
203     15:24:27      0: 3:48
204     15:29:38      0: 2:45
205     15:33:39      0: 2:56
206     15:36:51      0: 3:14
207     15:40: 0      0: 3: 3
208     15:42:16      0: 2:35
209     15:48:21      0: 2:50
210     15:51:33      0: 2:39
211     15:51:34      0: 1:33
212     15:54:51      0: 1:12
213     16: 1:18      0: 3: 6
214     16: 1:26      0: 2:44
215     16: 1:30      0: 2:55
216     16: 6:57      0: 1:34
217     16:13:26      0: 1:50
218     16:18:50      0: 2:16
219     16:19: 2      0: 2:38
220     16:25:10      0: 2:25
221     16:30:33      0: 2:26
222     16:33:50      0: 2:38
223     16:36:11      0: 2:59
224     16:40:22      0: 2:24
225     16:45:42      0: 2: 0
226     16:46: 3      0: 1:34
227     16:47:13      0: 3:47
228     16:51:17      0: 1:57
229     16:56:34      0: 2:15
230     17: 3: 1      0: 2:30
231     17: 8:25      0: 1:43
232     17: 9:35      0: 3: 8
233     17:15:58      0: 2:31
234     17:16: 5      0: 3:33
235     17:21:14      0: 1:20
236     17:23:42      0: 3:20
237     17:26:44      0: 2:19
238     17:27: 0      0: 1:54
239     17:29: 4      0: 3:54
240     17:35:25      0: 3: 7
241     17:38:31      0: 2:21
242     17:40:42      0: 3:48
243     17:44:11      0: 1: 8
244     17:49:13      0: 2:34
245     17:51:19      0: 3: 4
246     17:56:29      0: 2:23
247     18: 0:51      0: 3:22
248     18: 3: 9      0: 3:25
249     18: 8:37      0: 2:16
250     18:10: 3      0: 2:37



Mon, 13 Nov 2000 03:00:00 GMT  
 HELP!! on queues.....

Quote:

> The cust.inp file has 250 records. It is apparently real records from a
> retail store here in Melbourne (although the original file has 10,000+
> records).

well, as you could see in my follow up to your earlier post, a file with
just 21 records would call the delay() function roughly 3100 times now just
imagine how often you'd be delaying with 10 times the records...

Quote:

> Now my .h file is as follows:

[snip]

Quote:

> Now once again the program is as follows:

[snip]

Quote:

>      //clrscr() ;
[more snipped]
>      //window(1,1,80,25) ;

[even more snipped]

Quote:

> (Please note that I have remarked out the goto, clear screen etc)

you meant well but // style comments are not (yet) part of standard C even
though a few compilers support them. you should use /* */ style comments in
comp.lang.c

and remember: void main() will get sizzled in this group - adjust it before
you post your next program version.

/urs

--
Time is a great teacher, but unfortunately it kills all its pupils.
(H.Berlioz)
----------------------------------------------------------------------------
To reply by e-mail remove REMOVE. and .NOSPAM from my mail address



Mon, 13 Nov 2000 03:00:00 GMT  
 HELP!! on queues.....

Quote:

>Thanks for the advice Ken.

>In reguards to the other students, the student who had the program run
>in 4 seconds had really weird results, however the 2 girls that had
>their program finished in 30 seconds to a minute had almost identical
>results to me. As a reminder my program finishes in 20 minutes!!!

I've given the program a quick run, and three problems I've noticed:

1. The program only seems to simulate one serving point. In your
supplied data file, customer 12 should be served immediately, not wait
until 11 is done.

2. The program's slow because when there is a customer in the shop
time advances a second at a time. There are 37,720 iterations around
the main loop, so when you allow for the overhead of all the printing
you have a program that is very slow.

3. getave() doesn't work correctly. I make it that 126:57:11 / 250 =
0:30:28, not 0:29:48 as reported by the code. Hint: dividing an int by
an int gives an integer result, and anyway it would be better to
calculate the total number of seconds and divide by n, rather than
trying to process each of the hh, mm and ss on their own.

If your program used an algorithm that only examined significant
events, such as the one I suggested in my other post, you'd only need
about 500 iterations, 250 for the customers entering the shop, and 250
for the customers finishing being served. If we assume that with the
new algorithm the inner loop takes about the same amount of time to
run, this would cut the execution time down to about 16 seconds!

HTH

Quote:
>Petar Vucic.

Ken
--


#include <disclaim.h>

c.l.c. FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 13 Nov 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help With Linked Priority Queue

2. PLEASE HELP - Stack to Queue Conversion

3. Help with implementing a Priority Queue / Binary Heap!

4. Newbie needs help with circular queue

5. I need some help with queues

6. Need some help with a queue

7. Linked List-Queues/Stacks PLEASE HELP

8. Help: how to implement a queue?

9. Need help on initialising a queue node

10. HELP: problems with message queues

11. Please help -> circular queues

12. help with queues

 

 
Powered by phpBB® Forum Software