Thread: I heard Java's 'new' > C's 'malloc'

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    I heard Java's 'new' > C's 'malloc'

    Please C community, say it isn't so! If Java's 'new' is faster than 'malloc' then is there even any point in using C anymore?

    For example, I posted before about moving meshes and linked lists and stuff. I finally learned the proper scheme which actually has nothing to do with what I thought it would have to but in this code, I'm allocating a lot. Like, a lot a lot.

    Should I then just re-write all my code in Java or stick to C? Keep in mind, this code needs to be multithreaded for an arbitrary amount of threads and be able to run on a supercomputer cluster kind of thing. Does C exceed Java's ease-of-use and speed here and that's why I should stick with it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on how well you program in C and Java, and if any difference actually matters in your case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by MutantJohn View Post
    Please C community, say it isn't so! If Java's 'new' is faster than 'malloc' then is there even any point in using C anymore?
    You can get sometimes considerable speed up in C by writing your own special purpose allocators (see the chapter "memory games" in my book). For instance if you need a lot of objects all of the same, fairly small size, a block allocator can compile to a few machine instructions.

    C gives you control that Java can't.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay. I'm taking a Cartesian grid full of nodes and some of them will contain pointers to particle structures. This is a particle mesh so you have a mesh as a base allocation and then particles are allocated and placed in random points on the mesh.

    What I'm doing is, take each node that contains a particle and build a tetrahedra. You need 4 points to do this and the idea is, you build corresponding tetrahedra such that every particle node is a vertex of some tetrahedron. So I have a mesh structure, particle structures and then a tetrahedron quadtree. I wonder if I could instead remove the mesh structure and build the tetrahedron tree based off of solely the particle structures. I think I could, actually. But the physics of why keeping/removing the Cartesian mesh are beyond this forum.

    But now that you have a better idea of how much I'm allocating and managing, does Java do all this better? Its class system seems kind of backwards and icky to me.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Would Java be faster in terms of execution speed? Probably not.
    Would it be faster to use Java to implement this? Probably yes.
    Would it be easier to use Java to debug this? Probably yes.
    Also, malloc is often optimized for bigger allocations, not smaller ones.
    So there you have it. Pick your poison.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Code:
    struct particle
    {
       /* data you need for the particle (mass, velocity, 
           whatever */
       strcut particle *next; /* used for memory mangement*/
    };
    
    struct tetrahedron
    {
       int pi[4]; /*indices into the mesh */
    };
    
    struct particle *mesh[width][height][depth];
    struct tetrahedron *tets[MAXTETRAHEDRONS];
    int Ntetrahedrons;
    
    struct particle memorypool[MAXPARTICLES];
    Is that the system you want?
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Malcolm, that is very close, actually. Just add "struct tetrahedron *neighbours[4];" to the tetrahedron structure definition. What the code is supposed to, create a tetrahedron that contains all particles and then insert a particle one at a time. Each insertion is supposed to break a tetrahedron into 4 daughters, which is why I mentioned the quadtree because it's so similar in execution.

    So, this is formally called Delaunay triangulation and there is a lot of stuff done on it but only in 2D, it seems. I'm also using Springel 2009, titled "Galiliean-invariant cosmologicalhydrodynamical simulations on a moving mesh". So basically, he says that a tetrahedron is valid if the circumsphere around the vertices of the tetrahedra does not contain any other point from the set.

    I have code now that takes 4 vertices, inserts a new point and then creates 4 daughter tetrahedra. Each one excludes a unique point AND each one also passes the Delaunay-hood test. Are these therfore non-intersecting tetrahedra? So, we have 5 points. The old tetrahedron and the new insertion. The insertion requires 3 neighbours. We have 4 choices at first, 3 second and then 2 last. Therefore we have 4*3*2 = 24 unique cases. We sort tetrahedra by orientation and only keep the positive ones. Now we have 12. Each tetrahedra also needs to exclude a unique point so we remove degeneracy and now only have 4 cases. Degenerate in this case means valid tetrahedra that exclude the same point but are in a different order.

    I think what I have should be working as I exclude 4 unique points and each one passes but I'm not sure how to explicitly test for intersection.

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    C's malloc is as fast as your C library decides to make it. As far as the language itself goes, it can wait ten hours before returning a null pointer on every call. Either way, I suggest using whatever language you actually know.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MutantJohn View Post
    Please C community, say it isn't so! If Java's 'new' is faster than 'malloc' then is there even any point in using C anymore?
    With logic skills like that, your programs must work perfectly
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I must agree, that is a silly statement. If you don't like your C library's malloc(), C lets you write your own memory manager. That is one advantage of C over Java; more low-level control. If Java's new() is optimized to be super-fast, then it must make design trade-offs in terms of wasted space. All of the GC algorithms have these trade-offs, but with Java you're stuck with the one in your interpreter.
    Last edited by MacNilly; 04-24-2013 at 05:07 PM.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    Please C community, say it isn't so! If Java's 'new' is faster than 'malloc' then is there even any point in using C anymore?
    If your claim is true, then that speed of "Java's 'new'" is probably associated with increased memory usage and memory fragmentation. All real computers have finite memory and increased memory usage or fragmentation practically slows down programs in unpredictable ways. On that basis, is there any point in using Java anymore?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So my gcc is 4.8.0. Does that mean that my standard malloc is teh awesome? Or is there actually a faster one that I can download from somewhere? It'd be sweet if someone made a better memory manager than the people who make C though I doubt that that happens.

  13. #13
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    GCC is a C compiler, not a C library.

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    In a program where all the objects are only allocated one time, it could just do a single malloc, then just set the pointers for each object as offsets from the pointer returned from malloc (and only a single call to free when the program is done). I've seen this done a few times.

    Getting back to the original post, what is suppose to be better about new / malloc in Java versus C / C++, and is this enhanced new / malloc tied to a specific version of Java?

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by MutantJohn View Post
    Malcolm, that is very close, actually. Just add "struct tetrahedron *neighbours[4];" to the tetrahedron structure definition. What the code is supposed to, create a tetrahedron that contains all particles and then insert a particle one at a time. Each insertion is supposed to break a tetrahedron into 4 daughters, which is why I mentioned the quadtree because it's so similar in execution.

    So, this is formally called Delaunay triangulation and there is a lot of stuff done on it but only in 2D, it seems. I'm also using Springel 2009, titled "Galiliean-invariant cosmologicalhydrodynamical simulations on a moving mesh". So basically, he says that a tetrahedron is valid if the circumsphere around the vertices of the tetrahedra does not contain any other point from the set.

    I have code now that takes 4 vertices, inserts a new point and then creates 4 daughter tetrahedra. Each one excludes a unique point AND each one also passes the Delaunay-hood test. Are these therfore non-intersecting tetrahedra? So, we have 5 points. The old tetrahedron and the new insertion. The insertion requires 3 neighbours. We have 4 choices at first, 3 second and then 2 last. Therefore we have 4*3*2 = 24 unique cases. We sort tetrahedra by orientation and only keep the positive ones. Now we have 12. Each tetrahedra also needs to exclude a unique point so we remove degeneracy and now only have 4 cases. Degenerate in this case means valid tetrahedra that exclude the same point but are in a different order.

    I think what I have should be working as I exclude 4 unique points and each one passes but I'm not sure how to explicitly test for intersection.

    The equation of a plane is Ax + By + Cz + D = 0. x,y and z are the normals to the plane. If you enter a point x,y,z into the equation, it is 0 if it lies on the plane, positive if it is in front of it, and negative if it lies behind it. So as long as you get your normals the right way round, you can easily test if a point is within or without a tetrahedron.

    If two planes have different normals, they are not parallel and will intersect. There is a line which defines the intersection and (I hope I'm getting this right), a plane which contains the line and is normal to the other two planes. If you define a shape (e.g. triangle) on both planes, and they are all on opposite sides of this plane, the two do not intersect. I need to check that I'm right on this, but if I am, you can easily test for intersecting tetrahedra. Remember you have to handle the case of a tetrahedron which is contained within another separately.

    The memory problems are easily solved with a fixed block allocator. If you have a system of nested tetrahedra, you might also need "mother" and "child" tetrahedra.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heard of Monash?
    By zacs7 in forum Tech Board
    Replies: 4
    Last Post: 12-19-2007, 11:45 PM
  2. TCPA - anyone heard of it?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-25-2005, 08:08 AM
  3. has anyone heard of ezwin.h
    By snooper in forum Windows Programming
    Replies: 7
    Last Post: 05-19-2004, 11:17 AM
  4. has anybody heard about this?
    By Korn1699 in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2001, 07:56 AM