473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

vm optimizations

i asked something about java optimization before, but i gave a very poor
example... leading to not much help... so here is a better situation.

i have a Map, and i'm constantly picking entries from random from this
map.

in one method, i only use the map.
in another method, i use an array of keys from the map, stored seperately.

method a (only the map):

Map myMap = //initialize map
Object randObj;
// begin loop
randObj = myMap.keySet().toArray()[(int)Math.floor(Math.random() *
myMap.size())];

so, as you can see, in method a i constantly re-acquire the array of
keys... but reduce the clutter.

method b:

Map myMap = //initialize map
Object[] keyArray = myMap.keySet().toArray();
// begin loop
randObj = myMap.get(keyArray[(int)Math.floor(Math.random() *
keyArray.length)]);

the only change then is one single array that has a name.

what i'm wondering is:

does the compiler or runtime environment do any optimizations such that
method a is the same (or better maybe) than method b?

while i know the call stack will be one shorter in method b, i'm really
curious about whether or not method a constantly allocates and frees up
memory for the array.

does anyone know where i can find out about these sorts of issues, or can
anyone clear this example out for me?

thanks much,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
3 2209
> method a (only the map):

Map myMap = //initialize map
Object randObj;
// begin loop
randObj = myMap.keySet().toArray()[(int)Math.floor(Math.random() *
myMap.size())];

method b:

Map myMap = //initialize map
Object[] keyArray = myMap.keySet().toArray();
// begin loop
randObj = myMap.get(keyArray[(int)Math.floor(Math.random() *
keyArray.length)]);


There seems to be some inconsistency in the nature of the two methods.
While (a) populates randObj with random keys, (b) retrieves values
values based on random keys.
Call stack size wouldn't affect method (a) significantly as the
Map.keySet() method provides you with a collection view of the keys
and doesn't create a new Set as such. The execution efficiency of
method (b) depends on the Map implementation used; eg. a HashMap woul
aloow fster random access than a TreeMap.
You need to be a little clearer in your algorithm.
Jul 17 '05 #2
you're correct, i wrote the example wrong. you jist should've still been
clear... here is the revised line in method a:

randObj = myMap.get(myMap.keySet().toArray()[(int)Math.floor(Math.random()
* myMap.size())]);

(all that was needed was adding a myMap.get(..) statement in front of the
line.

murat

On Thu, 29 Oct 2003, Saager wrote:
method a (only the map):

Map myMap = //initialize map
Object randObj;
// begin loop
randObj = myMap.keySet().toArray()[(int)Math.floor(Math.random() *
myMap.size())];

method b:

Map myMap = //initialize map
Object[] keyArray = myMap.keySet().toArray();
// begin loop
randObj = myMap.get(keyArray[(int)Math.floor(Math.random() *
keyArray.length)]);


There seems to be some inconsistency in the nature of the two methods.
While (a) populates randObj with random keys, (b) retrieves values
values based on random keys.
Call stack size wouldn't affect method (a) significantly as the
Map.keySet() method provides you with a collection view of the keys
and doesn't create a new Set as such. The execution efficiency of
method (b) depends on the Map implementation used; eg. a HashMap woul
aloow fster random access than a TreeMap.
You need to be a little clearer in your algorithm.


--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #3
Murat Tasan wrote:
i asked something about java optimization before, but i gave a very poor
example... leading to not much help... so here is a better situation.

i have a Map, and i'm constantly picking entries from random from this
map.

in one method, i only use the map.
in another method, i use an array of keys from the map, stored seperately.

method a (only the map):

Map myMap = //initialize map
Object randObj;
// begin loop
randObj = myMap.keySet().toArray()[(int)Math.floor(Math.random() *
myMap.size())];

so, as you can see, in method a i constantly re-acquire the array of
keys... but reduce the clutter.

method b:

Map myMap = //initialize map
Object[] keyArray = myMap.keySet().toArray();
// begin loop
randObj = myMap.get(keyArray[(int)Math.floor(Math.random() *
keyArray.length)]);

the only change then is one single array that has a name.

what i'm wondering is:

does the compiler or runtime environment do any optimizations such that
method a is the same (or better maybe) than method b?

while i know the call stack will be one shorter in method b, i'm really
curious about whether or not method a constantly allocates and frees up
memory for the array.

does anyone know where i can find out about these sorts of issues, or can
anyone clear this example out for me?


Murat,

I cannot say anything for certain; but my expectation is that the
compiler optimizations will not yield the same or equivalent code here.
The main reason is that it is possible for another thread to change
the Map during the loop. If this happens, the behavior of the two
methods will be different.

BTW, a means of improving the performance of the first method without
changing the way it works (assuming that is desirable) would be to
allocate the array only once and re-use the array reference. E.g.

Object keyArr[] = new Object[myMap.size()];
for (;;)
{
keyArr = myMap.keySet.toArray(keyArr);
randObj = myMap.get(keyArr[(int)(Math.random()*keyArr.length]);
}

Ray

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VM | last post by:
Does Studio Dot Net include any optimizations that make it the appropriate tool for Pentium 4 processors? I'm writing a paper on compiler optimizations for modern hardware. Since I chose .Net...
6
by: william | last post by:
Could anyone tell me how to debug a Serviced Component (COM+) in .NET? I tried to attach the debugger to the host process, but failed, got error "unable to attach the process. There is no managed...
2
by: Patrick Stinson | last post by:
I've added an extra thread to my application read from an alsa midi device, and when I compile with any optimizations (gcc -O3 or gcc -O2) the application "freezes" (meaning I don't know what code...
1
by: VM | last post by:
Hello, I'm looking for information on C# compiler optimization or compilation for an engineering short paper I want to write. Any sites with some technical info on the new advances of C#...
1
by: Hareth | last post by:
VS2003: In the properties of the solution, their is a checkbox that lets you "enable optimizations". What are the pos & the neg?
9
by: Edmund Dengler | last post by:
Greetings! Just trying some tests out, and wanted to know about some optimizations. If I do a CHECK constraint on a table, is this used to optimize a SELECT or does Postgresql rely mostly on...
29
by: sammy | last post by:
Word up! If there are any gcc users here, maybe you could help me out. I have a program and I've tried compiling it with -O2 and -O3 optimization settings. The wired thing is that it actually...
0
by: tmh | last post by:
C++ optimizations (general) http://tmh-coding-tips.blogspot.com/2008/09/cc-c-optimizations-general.html C++ optimizations for initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.