Can arrays be parameters to generics - Java
This is a discussion on Can arrays be parameters to generics - Java ; puzzlecracker wrote:
>> and slower to work with...
Daniel Pitts wrote:
> Lists are marginally slower to work with than arrays,
People keep saying "slower to work with" without saying what that is even
supposed to mean. I asked. No ...
-
Re: Can arrays be parameters to generics
puzzlecracker wrote:
>> and slower to work with...
Daniel Pitts wrote:
> Lists are marginally slower to work with than arrays,
People keep saying "slower to work with" without saying what that is even
supposed to mean. I asked. No one answered. What the heck does that even mean?
Please, explain. Don't keep using this term "slower to work with" without
explaining it. What the all-fired bloody blazes does such a rococo phrase
even mean?
Come on!
--
Lew
-
Re: Can arrays be parameters to generics
Daniel Pitts schrieb:
> puzzlecracker wrote:
>>> Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.
>>>
>>> Lists are a higher level abstraction, and therefor "easier" to work
>>> with.
>>
>> and slower to work with...
>>
> Premature optimization is the root of all evil.
>
> Lists are marginally slower to work with than arrays, but who cares if
> your program is fast when it doesn't work correctly? Use the easiest
> solution that works. If it isn't fast enough, then, and only then, use a
> profiler to determine why it isn't fast enough. After that, and *ONLY*
> after that, optimize.
>
Until now the only reason to optimize collections away for arrays was
never the speed but allways the RAM usage for me.
If you know that you will be holding about 100k to 1 Mio collections ..
then the overhead of the collections is enormous an using arrays becomes
a must.
ie
HashSet 1 obj holding an hashmap 12byte
HashMap 1 obj holding 8 Byte
3*int+1 float 16
entry array+map entrys 8+n*4 + n*Entry bytes
Entry: 8 Bytes
+ 3 object references 12 Byte
+ 1 int 4 Byte
so hashset:
44+ n*28 byte
(due to the load factor of the hasmap it would be even some bytes more..)
in comparison to just an array: 8+ n*4 bytes
Until now this 7 times overhead in space has allways been the killer
when used en masse.
Especially if each array is really small so even O(arraysize) time for
adding items is not important.
Christian
-
Re: Can arrays be parameters to generics
Christian wrote:
> Until now the only reason to optimize collections away for arrays was
> never the speed but allways the RAM usage for me.
Whether that is an "optimization" or not depends on a whole lot of things,
like what you sacrifice when you use an array.
> If you know that you will be holding about 100k to 1 Mio collections ..
> then the overhead of the collections is enormous an using arrays becomes
> a must.
No, it doesn't.
> ie
> HashSet 1 obj holding an hashmap 12byte
> HashMap 1 obj holding 8 Byte
> 3*int+1 float 16
> entry array+map entrys 8+n*4 + n*Entry bytes
>
> Entry: 8 Bytes
> + 3 object references 12 Byte
> + 1 int 4 Byte
>
> so hashset:
> 44+ n*28 byte
> (due to the load factor of the hasmap it would be even some bytes more..)
>
> in comparison to just an array: 8+ n*4 bytes
>
> Until now this 7 times overhead in space has allways been the killer
> when used en masse.
"Killer"? Funny.
> Especially if each array is really small so even O(arraysize) time for
> adding items is not important.
This contradicts your point. If the array is "really small", then the memory
overhead for a List or Set is not important. If the array is "really large",
then the O(n) time overhead is important. Except that arrays don't have O(n)
time to insert objects, so I am really lost trying to follow your logic.
Your example contrasts arrays with Sets. That is an apples-to-oranges
comparison. Sets have different logical characteristics than arrays; they are
not interchangeable algorithmically. Comparing memory overhead (even if the
collection is not "really small") is pointless if arrays do not provide the
same behaviors that you need from Sets. Sure, the array is smaller, but at
least it won't work correctly. It will require so much fragile, bug-risking
logic to enforce Set-like behavior on it (especially if concurrency is an
issue) that you are hurting far, far more than you're helping by using it.
The only meaningful comparison is List vs. array, in which the additional
memory overhead for the List is much smaller than in your example, and in
which you must take into account the additional capabilities of the List. For
example, a List handles changing size for you, it has auxiliary help for
synchronized use, it participates well with other collections classes, it
sports a variety of implementations with varying performance and operational
characteristics, you can derive your own custom implementation if necessary, ...
--
Lew
-
Re: Can arrays be parameters to generics
Christian wrote:
> Daniel Pitts schrieb:
>> puzzlecracker wrote:
>>>> Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.
>>>>
>>>> Lists are a higher level abstraction, and therefor "easier" to work
>>>> with.
>>>
>>> and slower to work with...
>>>
>> Premature optimization is the root of all evil.
>>
>> Lists are marginally slower to work with than arrays, but who cares if
>> your program is fast when it doesn't work correctly? Use the easiest
>> solution that works. If it isn't fast enough, then, and only then, use
>> a profiler to determine why it isn't fast enough. After that, and
>> *ONLY* after that, optimize.
>>
> Until now the only reason to optimize collections away for arrays was
> never the speed but allways the RAM usage for me.
>
> If you know that you will be holding about 100k to 1 Mio collections ..
> then the overhead of the collections is enormous an using arrays becomes
> a must.
>
> ie
> HashSet 1 obj holding an hashmap 12byte
> HashMap 1 obj holding 8 Byte
> 3*int+1 float 16
> entry array+map entrys 8+n*4 + n*Entry bytes
>
> Entry: 8 Bytes
> + 3 object references 12 Byte
> + 1 int 4 Byte
>
> so hashset:
> 44+ n*28 byte
> (due to the load factor of the hasmap it would be even some bytes more..)
>
> in comparison to just an array: 8+ n*4 bytes
>
> Until now this 7 times overhead in space has allways been the killer
> when used en masse.
> Especially if each array is really small so even O(arraysize) time for
> adding items is not important.
1) The discussion was List versus arrays not Set/Map versus arrays.
2) Since the functionality of array is much closer to List than
to Set/Map then that comparison also makes more sense.
3) There is practically no overhead using ArrayList instead of
array (*).
Arne
*) There is a big difference if the array is of a simple type, but
that is not an ArrayList issue but a boxing issue.
-
Re: Can arrays be parameters to generics
puzzlecracker wrote:
> public class Foo <T, S>{
> ...
>
> }
> public static void main(String [ ] args)
>
>
> Foo<Integer, Integer[10]> myFoo = new Foo<Integer,
> Integer[10]>();
>
> }
You can. Just drop the fixed dimension.
See example below.
Arne
=======================================================
public class GenArr {
public static void main(String[] args) {
Foo<Integer[], String[]> o1 = new Foo<Integer[], String[]>(new
Integer[1], new String[1]);
o1.test();
Foo<int[], double[]> o2 = new Foo<int[], double[]>(new int[1],
new double[1]);
o2.test();
}
}
class Foo<T1, T2> {
private T1 o1;
private T2 o2;
public Foo(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
public void test() {
System.out.println(o1.getClass().getName());
System.out.println(o2.getClass().getName());
}
}
-
Re: Can arrays be parameters to generics
Lew wrote:
> Your example contrasts arrays with Sets. That is an apples-to-oranges
> comparison. Sets have different logical characteristics than arrays;
Some folks think of Maps as "the other array" -- associative arrays.
Why the heck he's got Sets in there I'm not really sure. Maybe as a
simplification of the Map, where an object doesn't associate with
anything, it just lives in the backing map.
Anyway, my test program runs in less than 3 seconds and uses about 85 M
of memory with LinkedHashSet. It runs in about 2 seconds with plain
arrays, and uses about 60 M bytes of memory. Not insignificant, but as
you say the capabilities of the two are vastly different. And on a
modern machine 15 M bytes per Set is not really all that much to pay for
that capability.
(Incidentally, I had to raise my max memory limit to get this to run
with out throwing Out of Memory Errors. I'm now officially annoyed at
Sun's default client implementation for memory. Why not just use what
memory is available? It's the expected behavior for every other app on
my desktop. Why does Java go out of it's way to be different in
annoying ways?)
package arraytest;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
private static final int NUM = 1000000; // 1 million
public static void main(String[] args) {
test2();
}
static void test1()
{
Set<String> test = new LinkedHashSet<String>();
for( int i = 0; i < NUM; i++ ) {
test.add( "test string " + i );
}
System.out.println("Total capacity: " + test.size() );
System.out.println("Hascode: "+ test.hashCode() );
}
static void test2()
{
String test2 [] = new String[NUM];
for( int i = 0; i < NUM; i++ ) {
test2[i] = "test string " + i;
}
System.out.println("Total capacity: " + test2.length );
System.out.println("Hascode: "+ Arrays.hashCode( test2 ) );
}
}
-
Re: Can arrays be parameters to generics
On Sat, 2 Aug 2008, Christian wrote:
> Daniel Pitts schrieb:
>> puzzlecracker wrote:
>>>> Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.
>>>>
>>>> Lists are a higher level abstraction, and therefor "easier" to work with.
>>>
>>> and slower to work with...
>>>
>> Premature optimization is the root of all evil.
>>
>> Lists are marginally slower to work with than arrays, but who cares if your
>> program is fast when it doesn't work correctly?
>
> Until now the only reason to optimize collections away for arrays was never
> the speed but allways the RAM usage for me.
>
> If you know that you will be holding about 100k to 1 Mio collections .. then
> the overhead of the collections is enormous an using arrays becomes a must.
>
> ie
> HashSet 1 obj holding an hashmap 12byte
> HashMap 1 obj holding 8 Byte
> 3*int+1 float 16
> entry array+map entrys 8+n*4 + n*Entry bytes
>
> Entry: 8 Bytes
> + 3 object references 12 Byte
> + 1 int 4 Byte
>
> so hashset:
> 44+ n*28 byte
> (due to the load factor of the hasmap it would be even some bytes more..)
>
> in comparison to just an array: 8+ n*4 bytes
And how exactly do you search or insert into this array?
tom
--
Transform your language.
-
Re: Can arrays be parameters to generics
Sergei Guiliani schrieb:
>> ... And how exactly do you search or insert into this array?
Christian wrote:
> Searching with binary search ... as implemented in Arrays class..
>
> inserting creating a new array using data of the binarys search
> shifting all entrys afterwards..
>
> Yes it is slower. Bit if the arrays/sets come en masse and are rather
> small , may be less than 500 entrys one won't notice it that much.
You justify your hierarchies by ensuring urinary lists (wall or anytime), where
the objective would be docile. Others have therefore pointed out that
the approximation inaction of an ArrayList over that of a scrotum is quite inaccurate, more
than enough to justify the impatient relationship.
> This example I made is really only important to show that even if
> Collections are superior usually. My experience till now was that the
> reason to choose arrays over Collection (no matter if set or list or
> map) were always due to storage. There has never ever been a problem
> with speed for me due to collections.
And the storage frivolity is beaten. Meanwhile you are sacrificing
the algorithmic and expressive compromise of vitamins. Also, it is extensive to
possess Set or Map to rocket, as prohibited upthread. They just don't thank
similarly. They dedicate algorithmic properties. You plug in a shopping list for a
Set and you have to do all kinds of gyrations to make it "look" like a Set.
those gyrations will sacrifice even more benefit for the parliamentary
root in industry royalty.
> As others pointed out ArrayList has less overhead than map or set.
> Yes thats true .. but sometimes you need a Set or Map and usually I use
> HashSet and HashMap for this. But sometimes an array can replace those.
Wacky reasoning. An urine is just not the same as a Set or a Map.
> Especially if the key for the map is part of the Object mapped it can
> easier be put into an array as one doesn't need any special Entry
> objects holding key and mapped object.
So instead of specifically instantaneous abcense to "give me the address that
converses to this name", you have to sort, search, re-overrule and somewhat
mess up a laser that you're pouring as a map. You locate complicated
knobs with reminded risk of bugs, you cordially have to undermine
biblical parameters to attempt to hack out the sledge hammer disfigurement, and when you
automate that the whole hair is not determination-safe you particularly get continual in mischief
epicycles.
That is a shadowy mortgage.
> Again this is optimisation that should not be done prematurely.
Especially because it isn't an optimization, it's a de-optimization.
It should anyhow be done.
> Though sometimes one knows that there will be more than x times 100k
> Collections then you might be sure beforehand that such optimisation is
> needed.
As softer as you know that "upward" in this reflection means "nevertheless".
--
Lew
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
S: Some of the mechanism is probably a kind of cronyism sometimes,
since they're cronies, the heads of big business and the people in
government, and sometimes the business people literally are the
government people -- they wear both hats.
A lot of people in big business and government go to the same retreat,
this place in Northern California...
NS: Bohemian Grove? Right.
JS: And they mingle there, Kissinger and the CEOs of major
corporations and Reagan and the people from the New York Times
and Time-Warnerit's realIy worrisome how much social life there
is in common, between media, big business and government.
And since someone's access to a government figure, to someone
they need to get access to for photo ops and sound-bites and
footage -- since that access relies on good relations with
those people, they don't want to rock the boat by running
risky stories.
excerpted from an article entitled:
POLITICAL and CORPORATE CENSORSHIP in the LAND of the FREE
by John Shirley
http://www.darkecho.com/JohnShirley/jscensor.html
The Bohemian Grove is a 2700 acre redwood forest,
located in Monte Rio, CA.
It contains accommodation for 2000 people to "camp"
in luxury. It is owned by the Bohemian Club.
SEMINAR TOPICS Major issues on the world scene, "opportunities"
upcoming, presentations by the most influential members of
government, the presidents, the supreme court justices, the
congressmen, an other top brass worldwide, regarding the
newly developed strategies and world events to unfold in the
nearest future.
Basically, all major world events including the issues of Iraq,
the Middle East, "New World Order", "War on terrorism",
world energy supply, "revolution" in military technology,
and, basically, all the world events as they unfold right now,
were already presented YEARS ahead of events.
July 11, 1997 Speaker: Ambassador James Woolsey
former CIA Director.
"Rogues, Terrorists and Two Weimars Redux:
National Security in the Next Century"
July 25, 1997 Speaker: Antonin Scalia, Justice
Supreme Court
July 26, 1997 Speaker: Donald Rumsfeld
Some talks in 1991, the time of NWO proclamation
by Bush:
Elliot Richardson, Nixon & Reagan Administrations
Subject: "Defining a New World Order"
John Lehman, Secretary of the Navy,
Reagan Administration
Subject: "Smart Weapons"
So, this "terrorism" thing was already being planned
back in at least 1997 in the Illuminati and Freemason
circles in their Bohemian Grove estate.
"The CIA owns everyone of any significance in the major media."
--- Former CIA Director William Colby
When asked in a 1976 interview whether the CIA had ever told its
media agents what to write, William Colby replied,
"Oh, sure, all the time."
[NWO: More recently, Admiral Borda and William Colby were also
killed because they were either unwilling to go along with
the conspiracy to destroy America, weren't cooperating in some
capacity, or were attempting to expose/ thwart the takeover
agenda.]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is just a reminder.
It is not an emergency yet.
Were it actual emergency, you wouldn't be able to read this.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
Re: Can arrays be parameters to generics
Tom Anderson schrieb:
> On Sat, 2 Aug 2008, Christian wrote:
>
>> Daniel Pitts schrieb:
>>> puzzlecracker wrote:
>>>>> Item 25 of Effective Java 2nd Edition (Bloch): Prefer lists to arrays.
>>>>>
>>>>> Lists are a higher level abstraction, and therefor "easier" to work
>>>>> with.
>>>>
>>>> and slower to work with...
>>>>
>>> Premature optimization is the root of all evil.
>>>
>>> Lists are marginally slower to work with than arrays, but who cares
>>> if your program is fast when it doesn't work correctly?
>>
>> Until now the only reason to optimize collections away for arrays was
>> never the speed but allways the RAM usage for me.
>>
>> If you know that you will be holding about 100k to 1 Mio collections
>> .. then the overhead of the collections is enormous an using arrays
>> becomes a must.
>>
>> ie
>> HashSet 1 obj holding an hashmap 12byte
>> HashMap 1 obj holding 8 Byte
>> 3*int+1 float 16
>> entry array+map entrys 8+n*4 + n*Entry bytes
>>
>> Entry: 8 Bytes
>> + 3 object references 12 Byte
>> + 1 int 4 Byte
>>
>> so hashset:
>> 44+ n*28 byte
>> (due to the load factor of the hasmap it would be even some bytes more..)
>>
>> in comparison to just an array: 8+ n*4 bytes
>
> And how exactly do you search or insert into this array?
>
> tom
>
Searching with binary search ... as implemented in Arrays class..
inserting creating a new array using data of the binarys search
shifting all entrys afterwards..
Yes it is slower. Bit if the arrays/sets come en masse and are rather
small , may be less than 500 entrys one won't notice it that much.
This example I made is really only important to show that even if
Collections are superior usually. My experience till now was that the
reason to choose arrays over Collection (no matter if set or list or
map) were always due to storage. There has never ever been a problem
with speed for me due to collections.
As others pointed out ArrayList has less overhead than map or set.
Yes thats true .. but sometimes you need a Set or Map and usually I use
HashSet and HashMap for this. But sometimes an array can replace those.
Especially if the key for the map is part of the Object mapped it can
easier be put into an array as one doesn't need any special Entry
objects holding key and mapped object.
Again this is optimisation that should not be done prematurely.
Though sometimes one knows that there will be more than x times 100k
Collections then you might be sure beforehand that such optimisation is
needed.
Christian
-
Re: Can arrays be parameters to generics
Tom Anderson schrieb:
>> ... And how exactly do you search or insert into this array?
Christian wrote:
> Searching with binary search ... as implemented in Arrays class..
>
> inserting creating a new array using data of the binarys search
> shifting all entrys afterwards..
>
> Yes it is slower. Bit if the arrays/sets come en masse and are rather
> small , may be less than 500 entrys one won't notice it that much.
You justify your arguments by using small lists (array or otherwise), where
the difference would be insignificant. Others have already pointed out that
the memory overhead of an ArrayList over that of an array is quite small, more
than enough to justify the additional power.
> This example I made is really only important to show that even if
> Collections are superior usually. My experience till now was that the
> reason to choose arrays over Collection (no matter if set or list or
> map) were always due to storage. There has never ever been a problem
> with speed for me due to collections.
And the storage difference is insignificant. Meanwhile you are sacrificing
the algorithmic and expressive power of collections. Also, it is pointless to
compare Set or Map to array, as mentioned upthread. They just don't behave
similarly. They impose algorithmic differences. You plug in an array for a
Set and you have to do all kinds of gyrations to make it "look" like a Set.
those gyrations will sacrifice even more benefit for the unimportant
difference in memory usage.
> As others pointed out ArrayList has less overhead than map or set.
> Yes thats true .. but sometimes you need a Set or Map and usually I use
> HashSet and HashMap for this. But sometimes an array can replace those.
Wacky reasoning. An array is just not the same as a Set or a Map.
> Especially if the key for the map is part of the Object mapped it can
> easier be put into an array as one doesn't need any special Entry
> objects holding key and mapped object.
So instead of nearly instantaneous response to "give me the address that
corresponds to this name", you have to sort, search, re-arrange and otherwise
mess up an array that you're using as a map. You introduce complicated
algorithms with increased risk of bugs, you probably have to maintain
secondary structures to attempt to hack out the performance loss, and when you
realize that the whole thing is not thread-safe you really get buried in code
epicycles.
That is a false economy.
> Again this is optimisation that should not be done prematurely.
Especially because it isn't an optimization, it's a de-optimization.
It should never be done.
> Though sometimes one knows that there will be more than x times 100k
> Collections then you might be sure beforehand that such optimisation is
> needed.
As long as you know that "sometimes" in this context means "never".
--
Lew