1

I have a class which gets data from data base and sends to UI to generate charts.

The execution time of the class is 14 seconds

    List listOfCLI =new ArrayList();
List listOfRRV =new ArrayList();
List listOfROP =new ArrayList();
List listOfACR =new ArrayList();
List listOfPIN =new ArrayList();
List listOfRIS =new ArrayList();
List listOfTAA =new ArrayList();
List listOfTAR =new ArrayList();
List listOfPHA =new ArrayList();
List listOfSAR =new ArrayList();
List listOfGRQ =new ArrayList();
List listOfADC =new ArrayList();

When i change the code to

 List<DataMS> listOfCLI =new ArrayList<DataMS>();
List<DataMS> listOfRRV =new ArrayList<DataMS>();
List<DataMS> listOfROP =new ArrayList<DataMS>();
List<DataMS> listOfACR =new ArrayList<DataMS>();
List<DataMS> listOfPIN =new ArrayList<DataMS>();
List<DataMS> listOfRIS =new ArrayList<DataMS>();
List<DataMS> listOfTAA =new ArrayList<DataMS>();
List<DataMS> listOfTAR =new ArrayList<DataMS>();
List<DataMS> listOfPHA =new ArrayList<DataMS>();
List<DataMS> listOfSAR =new ArrayList<DataMS>();
List<DataMS> listOfGRQ =new ArrayList<DataMS>();
List<DataMS> listOfADC =new ArrayList<DataMS>();

the execution time goes up to 17 seconds.. Why did addition of the Generic suggested by eclipse has such an adverse impact.

Also can you suggest the optimal way of using Generics to reduce the execution time

**Map<String, List> reqCLI =new HashMap<String, List>();
        Map<String, List> reqRRV =new HashMap<String, List>();
        Map<String, List> reqROP =new HashMap<String, List>();
        Map<String, List> reqACR =new HashMap<String, List>();
        Map<String, List> reqPIN =new HashMap<String, List>();
        Map<String, List> reqTAA =new HashMap<String, List>();
        Map<String, List> reqTAR =new HashMap<String, List>();
        Map<String, List> reqRIS =new HashMap<String, List>();
        Map<String, List> reqPHA =new HashMap<String, List>();
        Map<String, List> reqSAR =new HashMap<String, List>();
        Map<String, List> reqGRQ =new HashMap<String, List>();
        Map<String, List> reqADC =new HashMap<String, List>();

        List<Map<String, List>> listCLI=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listRRV=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listROP=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listACR=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listPIN=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listTAA=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listTAR=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listRIS=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listPHA=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listSAR=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listGRQ=new ArrayList<Map<String, List>>();
        List<Map<String, List>> listADC=new ArrayList<Map<String, List>>();**

these are the remaining collection's used

Nayeem
  • 681
  • 15
  • 35
  • List or List will be the same thing (List -> List) – Marco Acierno Mar 25 '14 at 18:10
  • 6
    Generics has no effect in execution time. Do you have benchmark results to prove your point? – Rohit Jain Mar 25 '14 at 18:11
  • Like i said execution time went up to 27 seconds after adding List listOfCLI =new ArrayList();in place of List listOfCLI =new ArrayList(); iam running it right now... As we speak – Nayeem Mar 25 '14 at 18:14
  • what JVM do you use to execute the code ? – Damian Leszczyński - Vash Mar 25 '14 at 18:18
  • I think the problem is somewhere else. The generic types won't make it to the JVM. Can you create a small benchmark which proves it? – NeplatnyUdaj Mar 25 '14 at 18:19
  • java version "1.6.0_45" Java(TM) SE Runtime Environment (build 1.6.0_45-b06) Java HotSpot(TM) Client VM (build 20.45-b01, mixed mode, sharing) – Nayeem Mar 25 '14 at 18:22
  • LinkedHashSetTest linkedTest = new LinkedHashSetTest(); long startTime = System.nanoTime(); System.out.println("Trend SysOut "+linkedTest.getReqTrendData("08/10/2013", "12/10/2013")); long endTime = System.nanoTime(); long duration = endTime - startTime; System.out.println("startTime"+startTime+"Time difference: " + duration); logger.debug("==Total Time of Exceution====" + duration/1000000000); – Nayeem Mar 25 '14 at 18:23
  • How much over head does a Data Base hit create? From JAVA to Oracle. – Nayeem Mar 25 '14 at 18:25
  • 1
    This is not a good benchmark since it contains too many souts inside which falsify what you really want to measure. – Meno Hochschild Mar 25 '14 at 18:27
  • http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java explains pitfalls in Java benchmarking well. – Mike Samuel Mar 25 '14 at 20:00

2 Answers2

2

Because of type erasure (see http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ101), generic types are not represented in the compiled bytecode, so there should be no difference in performance at execution time. The parameterized types are checked solely at compile time.

sumitsu
  • 1,481
  • 1
  • 16
  • 33
2

The only difference is that the java compiler can insert implicit casts:

List a = new ArrayList();
b.add("foo");
List b = new ArrayList();
for (Object o : a) {
  b.add(o);  // Adding an object.
}

has no casts, but there are implicit casts in

List<String> a = new ArrayList<String>();
b.add("foo");
List<String> b = new ArrayList<String>();
// Each element is implicitly cast to a String which requires
// a runtime type check.
for (String s : a) {
  b.add(s);
}

That's the only difference at the byte-code level.

It's hard to tell whether that would account for the discrepancy you're seeing without knowing the number of writes and reads that you're doing but RTTI checks are pretty cheap.


Preferring batch operations like List.addAll and Map.putAll instead of looping should eliminate that overhead if that is the culprit.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245