java - When select is faster than a for? -
java - When select is faster than a for? -
imagine situation, have id of entity , have liked list entities of same class. find the entity, when faster in linked list or select @ database?
i mean, when have 5 entities in linked list, example, imagine faster, , when have millions of entities select faster.
but when select starts faster for?
it faster after 1e5 elements on machine*. baseline selecting table 1 record trough jpa 1ms.
confidence interval (99.9%): [0,747, 1,087]
select 1e5 records take same time(1ms) lower bound increment 0.1ms.
sequential search in list of 1e5 elements desired element placed lastly takes 2ms.
confidence interval (99.9%): [2073669,347, 2514442,020]
ammortize dividing 2 random postition of desired element.
fyi getting entity map of 1e5 elements take less 30ns
here benchmark peer review:
entitymanagerfactory emf; list<individual> list; map<integer, individual> map; @setup public void setup() { emf = persistence.createentitymanagerfactory("postgres"); list = new arraylist<individual>(); map = new hashmap<integer, individual>(); (int = (int)1e5; > 0; i--) { individual individual = new individual(i); list.add(individual); map.put(i, individual); } } @teardown public void teardown() { emf.close(); } @benchmark @fork(1) @benchmarkmode(mode.averagetime) @outputtimeunit(timeunit.milliseconds) public individual measurejpa() { entitymanager em = emf.createentitymanager(); query query = em.createquery("select com.company.individual i.id = 1"); em.gettransaction().begin(); individual individual = (individual) query.getsingleresult(); em.gettransaction().commit(); em.close(); homecoming individual; } @benchmark @fork(1) @benchmarkmode(mode.averagetime) @outputtimeunit(timeunit.nanoseconds) public individual measurelist() { (individual : list) { if (i.id == 1) { homecoming i; } } throw new runtimeexception(); } @benchmark @fork(1) @benchmarkmode(mode.averagetime) @outputtimeunit(timeunit.nanoseconds) public individual measuremap() { homecoming map.get(1); }
dual core 1.5ghz cpu, 7200rmp hdd, 600mhz ram, windows 7 x64, jdk 1.8.0_05, eclipselink 2.5.1, postgessql 9.3 java database performance hibernate jpa
Comments
Post a Comment