Can some on help me optimize my code such that it can use less memory & be efficient

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    Can some on help me optimize my code such that it can use less memory & be efficient

    The following program was supposed to read a number of years from a file called "friday.in" then print out how many of the thirteenth were on each day from monday to sunday on to a file called "friday.out ". Here is the code which I used .
    Code:
    import java.io.*;
    import java.util.*;
    public class friday{
    	public static void main(String args[]) throws IOException{
    		
    		BufferedReader in = new BufferedReader(new FileReader("friday.in"));
    		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
    		int mon,tue,wed,thu,fri,sat,sun;
    		mon = tue = wed = thu = fri = sat = sun = 0;
    		int day = 13;
    		int year = 1900;
    		int years = Integer.parseInt(in.readLine());
    		while (year < year + years){
    				int numday;
    				int tot;
    				int month = 1;
    				switch (month){
    				case 1:tot = 31;break;
    				case 2:tot = (((year % 4)==0) || ((year % 100) == 0))?29:28;break;					
    				case 3:tot = 31;break;
    				case 4:tot = 30;break;
    				case 5:tot = 31;break;
    				case 6:tot = 30;break;
    				case 7:tot = 31;break;
    				case 8:tot = 31;break;
    				case 9:tot = 30;break;
    				case 10:tot = 31;break;
    				case 11:tot = 30;break;
    				case 12:tot = 31;break;
    				default:tot = 31;				
    				}
    				if (((year % 4)==0) || ((year % 100) == 0)){					
    					numday = 366;
    				}
    				else{					
    					numday = 365;
    				}
    				while (month <= 12){
    					int cat = day % 7;
    					if (cat == 1){
    						mon++;
    						}
    					else if (cat == 2){
    						tue++;
    						}
    					else if (cat == 3){
    						wed++;
    						}
    					else if (cat == 4){
    						thu++;
    						}
    					else if (cat == 5){
    						fri++;
    						}
    					else if (cat == 6){
    						sat++;
    						}
    					else{
    						sun++;
    						}
    					day += tot;
    					month++;
    					}
    				day = day % numday;
    				year++;
    				}
    		out.write(sat+" "+sun+" "+mon+" "+tue+" "+wed+" "+thu+" "+fri+"\n");
    		out.close();
    		System.exit(0);
    		}
    }
    The above code takes a long time to give an output and uses much more memory.
  • thatos
    New Member
    • Aug 2007
    • 105

    #2
    Here is the new version it runs quite fast but gives wrong values
    Code:
    import java.io.*;
    import java.util.*;
    public class friday{
    	
    	static int year = 1900;//The initial year
    	
    	//This method return the number of days in each month
    	public static int getNumDay(int month){
    		int tot;
    		switch (month){
    		case 1:tot = 31;break;
    		case 2:tot = (((year % 4)==0) || (((year % 100) == 0) && (year % 400 == 0)))?29:28;break;					
    		case 3:tot = 31;break;
    		case 4:tot = 30;break;
    		case 5:tot = 31;break;
    		case 6:tot = 30;break;
    		case 7:tot = 31;break;
    		case 8:tot = 31;break;
    		case 9:tot = 30;break;
    		case 10:tot = 31;break;
    		case 11:tot = 30;break;
    		case 12:tot = 31;break;
    		default:tot = 31;				
    		}
    		return tot;
    	}
    	public static void main(String args[]) throws IOException{
    		
    		BufferedReader in = new BufferedReader(new FileReader("E:/friday.in"));
    		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("E:/friday.out")));
    		int mon,tue,wed,thu,fri,sat,sun;
    		mon = tue = wed = thu = fri = sat = sun = 0;
    		int day = 13;//The initial day		
    		int years = Integer.parseInt(in.readLine());//The number of years
    		int totalYears = year + years - 1;
    		while (year < totalYears){
    				int numday;				
    				int month = 1;
    				//Calculates the number of days in that year
    				if (((year % 4)==0) || ((year % 100) == 0)){					
    					numday = 366;
    				}
    				else{					
    					numday = 365;
    				}
    				while (month <= 12){
    					int cat = day % 7;
    					if (cat == 1){
    						mon++;
    						}
    					else if (cat == 2){
    						tue++;
    						}
    					else if (cat == 3){
    						wed++;
    						}
    					else if (cat == 4){
    						thu++;
    						}
    					else if (cat == 5){
    						fri++;
    						}
    					else if (cat == 6){
    						sat++;
    						}
    					else{
    						sun++;
    						}
    					day += getNumDay(month);
    					month++;
    					}
    				day = numday % day;
    				year++;
    				}
    		out.write(sat+" "+sun+" "+mon+" "+tue+" "+wed+" "+thu+" "+fri+"\n");
    		out.close();
    		System.exit(0);
    		}
    }

    Comment

    • thatos
      New Member
      • Aug 2007
      • 105

      #3
      Latest version
      Code:
      import java.io.*;
      import java.util.*;
      public class friday{
      	
      	static int year = 1900;//The initial year
      	public static boolean isLeapYear(int year){
      		if ((year % 100 == 0)){
      			if ((year % 400) == 0){
      				return true;
      			}
      			else {
      				return false;
      			}
      			}
      		else if((year % 4) == 0){
      			return true;
      		}
      		else{
      			return false;} 
      	}
      	
      	//This method return the number of days in each month
      	public static int getNumDay(int month){
      		int tot;
      		switch (month){
      		case 1:tot = 31;break;
      		case 2:if (isLeapYear(year) == true){
      			tot = 29;
      		}else{
      			tot = 28;
      		}
      			break;					
      		case 3:tot = 31;break;
      		case 4:tot = 30;break;
      		case 5:tot = 31;break;
      		case 6:tot = 30;break;
      		case 7:tot = 31;break;
      		case 8:tot = 31;break;
      		case 9:tot = 30;break;
      		case 10:tot = 31;break;
      		case 11:tot = 30;break;
      		case 12:tot = 30;break;
      		default:tot = 31;break;				
      		}
      		return tot;
      	}
      	public static void main(String args[]) throws IOException{
      		
      		BufferedReader in = new BufferedReader(new FileReader("friday.in"));
      		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
      		int mon,tue,wed,thu,fri,sat,sun;
      		mon = tue = wed = thu = fri = sat = sun = 0;
      		int day = 13;//The initial day		
      		int years = Integer.parseInt(in.readLine());//The number of years
      		int totalYears = year + years;
      		int numday;
      		while (year < totalYears){
      								
      				int month = 1;
      				
      				//Calculates the number of days in that year
      				if (isLeapYear(year) == true){
      					numday = 366;
      				}
      				else{
      					numday = 365;
      				}
      				while (month <= 12){
      					int cat = day % 7;
      					if (cat == 1){
      						mon++;
      						}
      					else if (cat == 2){
      						tue++;
      						}
      					else if (cat == 3){
      						wed++;
      						}
      					else if (cat == 4){
      						thu++;
      						}
      					else if (cat == 5){
      						fri++;
      						}
      					else if (cat == 6){
      						sat++;
      						}
      					else{
      						sun++;
      						}
      					day += getNumDay(month);
      					month++;
      					}
      				day += numday;
      				year++;
      				}
      		out.write(sat+" "+sun+" "+mon+" "+tue+" "+wed+" "+thu+" "+fri+"\n");
      		out.close();
      		System.exit(0);
      		}
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Why don't you use a GregorianCalend ar and leave those nasty details to
        that class? In the outer loop you set the year, the inner loop sets the months
        and the day (13) and all you have to do is update the day counters.

        kind regards,

        Jos

        ps. I don't like large main() methods.

        Comment

        • thatos
          New Member
          • Aug 2007
          • 105

          #5
          Originally posted by JosAH
          Why don't you use a GregorianCalend ar and leave those nasty details to
          that class? In the outer loop you set the year, the inner loop sets the months
          and the day (13) and all you have to do is update the day counters.

          kind regards,

          Jos

          ps. I don't like large main() methods.
          I was requested not to use any java build in function of time to write a program which will compute this, nut any way I finaly fiqured out were my problem was.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by thatos
            I was requested not to use any java build in function of time to write a program which will compute this, nut any way I finaly fiqured out were my problem was.
            You should have googled a bit more; I did and I found pages about Zeller's
            congruence: a simple formula that calculates the Julian day number also used
            by the Gregorian calendar class. I hacked a bit and cooked up this:

            [code=java]
            public static int dayOfWeek(int y, int m, int d) {

            d+=(m<3)?y--:y-2;
            return (23*m/9+d+4+y/4-y/100+y/400)%7;
            }
            [/code]

            This little method returns 0 == Sun, 1 == Mon, ... 6 == Sat. Have fun with it.

            kind regards,

            Jos

            Comment

            Working...