Results 1 to 5 of 5
  1. #1
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default Fast array for insertion/removal

    In Java is there any existing class that stores 10-1000+ values, has fast insertion and removal of values without allocating, is preferably growable, and is contiguous in memory? If not is there anything close?

    Just trying to find out if I need to roll my own like I have in C++ or if Java already provides something that would be suitable.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In Java is there any existing class that stores 10-1000+ values, has fast insertion and removal of values without allocating, is preferably growable

    Look at the classes implementing java.util.List

    and is contiguous in memory?

    Better yet, don't. This isn't doable in Java. The langauge and its many API have no constructs referring to this "memory" of which you speak.

  3. #3
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Look at the classes implementing java.util.List
    Cool I'll check out the java.util.List classes, even if there's nothing useful now it's good to know what's available for later :).

    Quote Originally Posted by pbrockway2 View Post
    Better yet, don't. This isn't doable in Java. The langauge and its many API have no constructs referring to this "memory" of which you speak.
    *sigh*, more and more it's becoming obvious that Java just isn't suited for real time or time critical applications, JIT compilers are great but the language still prevents it from being comparable in both speed and efficiency to C++ code, which is a real pain when you are programming for platforms with limited CPU and RAM specs like phones.
    I should probably just forget about Java and switch to C++ with the Android NDK for the 100-1000 times speed increases I can get, and the extra RAM I'll have access to (that ironically I wouldn't need in C++), but I wouldn't learn anything if I just take the easy way out ;).

    Thanks for the help though :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Skiller View Post
    I should probably just forget about Java and switch to C++ with the Android NDK for the 100-1000 times speed increases I can get, and the extra RAM I'll have access to (that ironically I wouldn't need in C++), but I wouldn't learn anything if I just take the easy way out ;).
    A 100 - 1000 times speed increase? What JVM are you trying to run your classes on? Avery half self respecting JVM does at least some form of JIT compilation and the result is raw machine code. If you really have to, create an ArrayList with a capacity large enough that no reallocations will be necessary. A bit of tuning can help big times.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    A 100 - 1000 times speed increase? What JVM are you trying to run your classes on? Avery half self respecting JVM does at least some form of JIT compilation and the result is raw machine code. If you really have to, create an ArrayList with a capacity large enough that no reallocations will be necessary. A bit of tuning can help big times.

    kind regards,

    Jos
    I'm running it on the Android JVM which I believe is Dalvik, before Android version 2.2 it didn't have a JIT compiler so on the older versions the hit is really bad. It's not really Java in general that would give the speed increases though, just some bloated classes that I need to use in Java that are by far my largest bottleneck and one of them has a bug that makes things even slower. Using native code you can just use a memcpy instead of doing a heap of redundant stuff and iterating over large amounts of primitives like these classes do. Here's a link to the specific problem in case you are interested: http://www.badlogicgames.com/wiki/in...er.put_is_slow

    The other thing that would net me some further speed increases by going to C++ is that on Android apps are limited to only 16mb or 24mb of heap memory in Java, which means if I go to C++ I won't have those memory restrictions and can use the extra memory to store precomputed data rather than recalculating it one or in some cases many times each frame. I also wouldn't have garbage collection at run time, and with the generally faster code that comes from C++ anyway it can really add up.

    But yer, more to do with Java on Android than Java it's self so no need to worry :).
    Last edited by Skiller; 02-20-2011 at 03:25 PM.

Similar Threads

  1. Help me with this array element insertion
    By javanew in forum New To Java
    Replies: 4
    Last Post: 09-07-2010, 02:49 PM
  2. Complete Java Removal
    By jpChris in forum New To Java
    Replies: 14
    Last Post: 05-25-2010, 12:46 PM
  3. Adhoc selection & removal of JLabels
    By dan0 in forum AWT / Swing
    Replies: 5
    Last Post: 03-10-2009, 06:31 PM
  4. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  5. Removal of Homework Requests
    By CaptainMorgan in forum Suggestions & Feedback
    Replies: 14
    Last Post: 08-03-2008, 09:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •