Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hw12 refactoring to factory method

743 views

Published on

performance of factory method in java

Published in: Design, Technology, Sports
  • Be the first to comment

  • Be the first to like this

Hw12 refactoring to factory method

  1. 1. UTAH STATE UNIVERSITY COMPUTER SCIENCE CS-7350 Encapsulate Classes with Factory, based on “Refactoring to Patterns” Chapter 6 by Kerievsky, J. Jorge Edison Lascano Spring 2012 02-24-2012Encapsulate classes with factory, a brief performance analysis.According to Kerievsky, thisrefactoring has benefits from the programmer point of view: “simplify the creation of kinds ofinstances”, “reduces conceptual weight”, and “enforce the perform to an interface, not to animplementation from GoF”; nevertheless, a performance in creating objects is not discussed. Forthat reason and following his mechanics I created an example to create Bird(s), a subclass ofAnimal: 1) I implemented public static createBird(…) creation method, then I moved it toAnimal, 2) I called it from main as AnimalRefactor.createBird(…), 3) I declared theconstructor protected (see code).To test performance, I instantiated 1.000, 10.000 and 100.000objects to take 10 measure values and then I simply averaged them.Here the results inmilliseconds for Bird and BirdFactory, and Reason: 1.000 >0.0102, 0.0124, 6/5 ->Bird isfaster; 10.000 >0.0244, 0.0109, 2.2/1 Bird Factory is faster; 100.000 > 0.1029, 0.0559, 1.8/1Bird Factory is faster. These measures cannot be considered conclusive, but just ademonstration that a Factory is faster in this example under Netbeans7.1+ java1.7.0_01CODE AND MEASURESORIGINAL CODEpackage javaanimalfm;/** * * @author elascano*/public class Animal { private String name=""; private int birthDay=0; private int birthMonth=0; private int bithYear=0; private int code=0; public Animal(String n, int d, int m, int y, int c){ name=n; birthDay=d; birthMonth=m; bithYear=y; code=c; }}class Bird extends Animal{ boolean fly=false; int hatchFrequency=0; int hatchTemperature=0; boolean commercialEggs=false;
  2. 2. int furculaSize=0; public Bird(String n, int d, int m, int y, int c, boolean f, int h, int t, boolean e, int s){super(n,d,m,y,c);fly=f; hatchFrequency=h; hatchTemperature=t; commercialEggs=e; furculaSize=s; }}CODE REFACTORED TO FACTORY METHODpackage javaanimalfm;/** * * @author elascano */public class AnimalRefactor { private String name=""; private int birthDay=0; private int birthMonth=0; private int bithYear=0; private int code=0; public AnimalRefactor(String n, int d, int m, int y, int c){ name=n; birthDay=d; birthMonth=m; bithYear=y; code=c; } protected AnimalRefactor(){ name=""; birthDay=0; birthMonth=0; bithYear=0; code=0; } public static AnimalRefactor createBird(String n, int d, int m, int y, int c, boolean f, int h, int t,boolean e, int s){ return new BirdRefactor(n,d,m,y,c,f,h,t,e,s); }}class BirdRefactor extends AnimalRefactor{ boolean fly=false; int hatchFrequency=0; int hatchTemperature=0; boolean commercialEggs=false; int furculaSize=0; protected BirdRefactor(String n, int d, int m, int y, int c, boolean f, int h, int t, boolean e, int s){super(n,d,m,y,c);fly=f; hatchFrequency=h; hatchTemperature=t; commercialEggs=e; furculaSize=s; }}MAIN CLASS THAT CALLS THE ORIGINAL AND THE REFACTORED CODEpackage javaanimalfm;import java.util.ArrayList;import java.util.List;
  3. 3. /** * * @author elascano*/public class JavaAnimalFMMain { /** * @param args the command line arguments */ public static void main(String[] args) { int count = 1000000; long startTime; long stopTime; //LIST OF ANIMALS List<Animal> list_of_animals = new ArrayList<>(); startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { list_of_animals.add(new Animal("dog" + i, 5, 5, 2012, i)); } stopTime = System.currentTimeMillis(); System.out.println("Avg Time by creating " + count + " " + Animal.class.getSimpleName() + "(s) = " + ((stopTime - startTime) / 1000f) + " msecs"); //LIST OF BIRDS CREATED DIRECTLY List<Bird> list_of_birds = new ArrayList<>(); startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { list_of_birds.add(new Bird("dog" + i, 5, 5, 2012, i, true, 5, 5, true, 5)); } stopTime = System.currentTimeMillis(); System.out.println("Avg Time by creating " + count + " " + Bird.class.getSimpleName() + "(s) = " + ((stopTime - startTime) / 1000f) + " msecs"); //LIST OF BIRDSREFACTOR CREATED DIRECTLY List<BirdRefactor> list_of_birdsR1 = new ArrayList<>(); startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { list_of_birdsR1.add(new BirdRefactor("dog" + i, 5, 5, 2012, i, true, 5, 5, true, 5)); } stopTime = System.currentTimeMillis(); System.out.println("Avg Time by creating" + count + " " + BirdRefactor.class.getSimpleName() + "(s)REFACTOR BY ITSELF = " + ((stopTime - startTime) / 1000f) + " msecs"); //LIST OF BIRDSREFACTOR THROUGH INTERFACE FACTORY METHOD List<BirdRefactor> list_of_birdsR3 = new ArrayList<>(); startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { list_of_birdsR3.add((BirdRefactor) AnimalRefactor.createBird("dog" + i, 5, 5, 2012, i, true, 5, 5,true, 5)); } stopTime = System.currentTimeMillis(); System.out.println("Avg Time by creating" + count + " " + BirdRefactor.class.getSimpleName() + "(s)THROUGH INTERFACE = " + ((stopTime - startTime) / 1000f) + " msecs"); }}MEASURES
  4. 4. 1.000 BIRDS 1.000 BIRDS BY FACTORY 1 0.0130 0.0120 2 0.0100 0.0120 3 0.0120 0.0170 4 0.0090 0.0120 5 0.0100 0.0120 6 0.0100 0.0080 7 0.0090 0.0090 8 0.0100 0.0100 9 0.0100 0.0200 10 0.0090 0.0120 REASONAVERAGE 0.0102 0.0124 1.215686 Birds better 10.000 BIRDS 10.000 BIRDS BY FACTORY 1 0.0190 0.0110 2 0.0200 0.0100 3 0.0200 0.0100 4 0.0210 0.0100 5 0.0320 0.0130 6 0.0500 0.0110 7 0.0200 0.0100 8 0.0200 0.0130 9 0.0190 0.0100 10 0.0230 0.0110 REASONAVERAGE 0.0244 0.0109 2.238532 Birds Factory better 100.000 BIRDS 100.000 BIRDS BY FACTORY 1 0.0860 0.0450 2 0.0830 0.0730 3 0.0870 0.0430 4 0.0970 0.0490 5 0.1040 0.0470 6 0.1020 0.0510 7 0.1480 0.0570 8 0.0870 0.0750 9 0.0850 0.0760 10 0.1500 0.0430 REASONAVERAGE 0.1029 0.0559 1.840787 Birds Factory better

×