473,698 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(keyAr ray[(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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu

Jul 17 '05 #1
3 2224
> 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(keyAr ray[(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().toArr ay()[(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(keyAr ray[(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.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.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(keyAr ray[(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.to Array(keyArr);
randObj = myMap.get(keyAr r[(int)(Math.rand om()*keyArr.len gth]);
}

Ray

Jul 17 '05 #4

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

Similar topics

1
1312
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 as my main tool, I was looking for some sites that explain some of the optimizations .Net may have that will make it more compatible with modern hardware. I'm also looking for info on the differences between traditional compilers and the Dot Net...
6
6211
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 code running in the process. In order to attach to a process with the .NET debugger, managed code must be running in the process before attaching." Thanks
2
1079
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 is still executing) after the last of my threads (I have three, including the main thread). This does not happen w/o optimizations. Cheers
1
1651
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# compilation would be greatly appreciated. Any sites that have comparisons between C# compilation an any other language would be even better. My email is : MYERSV at GO dot COM
1
1650
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
3426
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 normal index search? For example, I want to create some tables to manage different data in a kind of <object, relationship, object2> manner, but where object2 could be an IP address, text, a number, etc. So I thought of doing the following:
29
1906
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 runs faster with -O2 than with -O3, even though -O3 is a higher optimization setting. Have I found a bug in gcc? Could I be doing something wrong?
0
1107
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 http://tmh-coding-tips.blogspot.com/2008/09/cc-c-optimizations-for-initialization.html C++ optimizations for loops, if/else and switch/case statements http://tmh-coding-tips.blogspot.com/2008/09/cc-c-optimizations-for-loops-ifelse-and.html C/C++ Tutorials
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.