Arrays in for loops

I don't know how I forgot to point that out!

I have a real-life case where this was crucial. We were in a AI class where we had to implement some form of greedy, heuristic-based search. I don't remember the details (it was 5 years ago), but I do remember this:

  • The base program allocated and deallocated lots of memory very quickly
  • The program was written in Java

The program ran out of memory because the garbage collector couldn't react to the speed at which memory needed to be deallocated. There was a better way to implement it? Surely. I don't remember the details.

But my takeaway from that is exactly what you said: more precise / deterministic control over memory is highly desirable for certain use-cases. I know that program would've ran fine in C or Rust. In Java, it quickly ran out of resources.

4 Likes

100% this. There are some language features that make optimizing harder/easier, but itā€™s all about control and determinism (to some extent), as @Ixrec put it.

Java is actually pretty fast, once JITā€™d (and to some degree, if written a certain way). And itā€™s fast to the point where choice of algorithm, data structure, or even small optimizer miscues can make it run faster or slower than C++ or Rust. Itā€™ll use a lot more memory for the same working set, thatā€™s pretty much a given.

And Iā€™d venture itā€™s very much non-trivial to write a large system where you can beat Java performance (throughput wise), particularly servers.

7 Likes

All of this conversation is sorta off topic and makes it slightly confusing for people searching for "array" or "for loop"

Just my two cents

6 Likes