this code is *SLOW*
Hi,
This code counts the number of work days betw two dates (it works) , but when I inserted two lines for debugging purposes and it took 45 seconds to execute. Remove them and response time is good.
// right now (hard codes values for testing)...
int var_thisYear = 1992;
int var_thisMonth= 10;
int var_thisDay = 1;
GregorianCalendar weekday =new GregorianCalendar(var_thisYear,var_thisMonth,var_thisDay);
Date var_weekday = weekday.getTime();
DateFormat wkf =new SimpleDateFormat("EEEEEEEEE");
String str_weekday = wkf.format(var_weekday);
int wkday = 0;// initize counter for weekdays...
System.out.println("===> weekday: " + str_weekday);
while (var_weekday.before(var_lstCouponDate))
{if (!str_weekday.equals("Saturday") || !str_weekday.equals("Sunday")){wkday++;}
weekday.add(GregorianCalendar.DATE,1);
var_weekday = weekday.getTime();
str_weekday = wkf.format(var_weekday);// <- HERE, remove it and response time is good
System.out.println("===> number of weekdays: "+ str_weekday);// <- HERE
}
System.out.println("===> number of weekdays: " + wkday);
[1896 byte] By [
acinhk] at [2007-9-30 3:43:49]

This is an example of faster code to calculate the weekday.
It just take less that 1 seconds on my pc
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class Test {
public static void main(String [] args) {
try {
long a_day = 24 * 60 * 60 * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
long date1 = sdf.parse("01-10-1992").getTime();
long date2 = sdf.parse("19-03-2004").getTime();
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date1);
int day = gc.get(gc.DAY_OF_WEEK);
int no_of_day = 0;
while (date1 <= date2)
{
if (day > 1 && day < 7)
no_of_day ++;
if (++day > 7)
day = 1;
date1 += a_day;
}
System.out.println("No of Weekday = " + no_of_day);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
I executed this code on both windows (1.4.2) and linux (1.4.1) and the response time is next to nothing. So your code is fine (allthough I had to remove the while loop because I don't know what the var_lstCouponDate is supposed to be).
> I executed this code on both windows (1.4.2) and linux
> (1.4.1) and the response time is next to nothing. So
> your code is fine (allthough I had to remove the while
> loop because I don't know what the var_lstCouponDate
> is supposed to be).
you removed the whole loop ? then how it going to calculate the no of weekday between 2 date ?
I think op wanted to calculate no of week day between 2 date...
Hi.Thanks for your time and the code.Actually, if I remove those 2 lines (they are only for testing and debugging) everything is fine again, the snippet is only part of a very long applet. Wonder why though...THX :-)
acinhk at 2007-6-29 14:51:37 >

> Hi.
> Thanks for your time and the code.
> Actually, if I remove those 2 lines (they are only for
> testing and debugging) everything is fine again, the
> snippet is only part of a very long applet.
> Wonder why though...
> THX :-)
i understand that System.out.print() used for debugging and testing purpose...
but by removing the 1st line the code still work correctly ?
I think the code was slow because it format the date for every loop. This is quite time consuming...
// right now (hard codes values for testing)...
int var_thisYear = 1992;
int var_thisMonth= 10;
int var_thisDay = 1;
GregorianCalendar weekday = new
new
GregorianCalendar(var_thisYear,var_thisMonth,var_thisDa
);
Date var_weekday = weekday.getTime();
DateFormat wkf = new
new SimpleDateFormat("EEEEEEEEE");
String str_weekday = wkf.format(var_weekday);
int wkday = 0; // initize counter for weekdays...
System.out.println("===> weekday: " +
" + str_weekday);
while (var_weekday.before(var_lstCouponDate))
{ if (!str_weekday.equals("Saturday") ||
|| !str_weekday.equals("Sunday")) {wkday++;} < str_weekday was used here
weekday.add(GregorianCalendar.DATE,1);
var_weekday = weekday.getTime();
str_weekday = wkf.format(var_weekday);
day);// <- HERE, remove it and response (the code still work correctly my removing this)
time is good
System.out.println("===> number of weekdays: "+
s: "+ str_weekday);// <- HERE
}
System.out.println("===> number of weekdays: " +
" + wkday);
Sorry Joe, I meant after commenting out System.out.println only, the reponse time becomes acceptable, actually surprisingly good considering the other things it is doing...
I tried it again, with and without System.out.println inside the loop.
Same result, very slow with and acceptable without.
acinhk at 2007-6-29 14:51:37 >

You're saying if you leave in this line str_weekday = wkf.format(var_weekday);
And take out this one System.out.println("===>number of weekdays: "+ str_weekday);
it's fast? If so, well... YOU LIE!! Just kidding, but I'm skeptical. Perhaps you're saying it's the other way around.
BTW I'm thinking the code you actually posted won't compile. Any chance what you're running is slow because there's some other difference?
nasch at 2007-6-29 14:51:37 >
