HI!!
Could someone please, please, please help.....
This is my final assignment for C at university. THe program runs well
but VERY slowly. The assignement involves setting up a queue, reading
data in and giving results of the amount of time (on average etc), it
takes for customers to get served. Ok DONE!
My program however runs SO SLOW, it counts each second (a real second)
as a customer enters my queue. This results in my program finishing in
20 minutes! Other students have there program's finishing in 8 seconds
to 3 minutes!
I have a problem I think with my inside loop, I have tried many
different variations of it which all give crazy results.
Here is my program & .h file...
ANy help would be great...
THE PROGRAM
------------------------------------------------------------------------
#include "a:\custq.h"
/*This is my final assignment. The program reads in information from a
cust.inp file and then works out how long it rakes for customers to get
served. It uses a queue and then prints out the final result (on
screen)*/
/*Note Goto etc have been remarked out as I am working something
out...*/
int main()
{
queue q ;
cust a ;
FILE *inf ;
char line[80] ;
int i, nocust = 0, currcust=0, currserv=0, xloc, yloc, delay =
0 ;
timerec currtime, currwait, fivesec ;
timerec avgwait, avgserv, totalwait, totalserv ;
/* clrscr() ; */
inf = fopen("a:\cust.inp","r") ;
create(&q) ;
while (fgets(line,80,inf) != NULL)/*read the file*/
{
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) ; /*insert into the queue*/
nocust++ ;
} ;
set_time(&currtime,6,59,30) ; /*Time set. Has to be this time as it
will give wrong results (shop opens at 7.00am)*/
set_time(&totalserv,0,0,0) ;
set_time(&totalwait,0,0,0) ;
set_time(&fivesec,0,0,5) ; /*If used it skips some records...*/
/* window(1,1,80,25) ; */
while (!empty(q)) /*While queue not empty......*/
{
/* 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) ;
/*When comparing the time with the arrival time of new customer, do
the following.... if it is the same....*/
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 ; /*I have tried many
combinations of this with no real change in speed*/
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) ; */
printf("Delay %d\n",delay++); /*Just to see whats happening*/
}
/*The final output....*/
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) ;
return (0)}
MY .h FILE IS AS FOLLOWS....
/*
*/
/* Include required system header and definition
*/
/*
*/
#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:
}/*end .h file*/
-----------------------------------------------------------------------
NOW could someone PLEASE give me some assistance for this problem I
have. AS can be seen, the .h file doesn't require any modification. I
have been told (on this newsgroup) that I should encorporate all this as
one program, h/e the lecturer wants the program run in this way.
I have posted this before (2 weeks ago), but have had some dramas the
last couple of weeks and this Sunday was the first oppurtunity I had in
looking at the program again.
I think the 'rules' for this newsgroup is that as a student I have to
prove that I have done the work, and that I understand it. I think the
above PROVES that.
If anyone could suggest any modification to the inside loop of the
program (which is where I believe the speed problem is), that would be
great. I have also thought of creating another queue, do you believe
that this is another solution for speed.
I WOULD love some assistance in this. I feel that the problem lies in
the inside loop "while(!empty(q))". It keeps incrementing the time,
second, by second, until the incremented time equals the arrival time of
the next customer.
THANK YOU in advance, many times over, any u can have my 1st born
child.....
Petar Vucic (University student, stressed, tired, etc)
PS (Assignment due Friday)
PPS (Exams start Monday)
PPPS (World Cup starts Wednesday.....)
PPPS Attached to this is the cust.inp file.....
[
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