Question:
Java question. Do methods make programs run faster?
Mr.G
2013-05-02 12:08:16 UTC
In Java do Methods make a program run faster? I'm just beginning and curious.
Five answers:
Isaac H
2013-05-02 12:16:06 UTC
Do you mean as opposed to having everything in one method or in the main method? In general, no. If you're just using one class and running it from top to bottom, methods wont increase speed. However, the idea in Java is to reduce how much code you have to repeatedly type, by referring to a class that youve already coded, creating objects of that type, and then using the methods available in that class.
green meklar
2013-05-03 01:28:19 UTC
No. In fact, most programs would actually run faster if all their code were crammed together into a single method.



However, a lot of the time, you care more about elegance and reusability than you do about speed. As a result, most of your code should be modularized (using methods, objects, inheritance, etc) in order to make life easier for you and anyone else who has to deal with the code.



In a few cases, you may find that there is a substantial speed boost to be gained by cramming some code together into one method, in which case, go ahead. Remember, the code that runs once every second, or even a hundred times every second, should be elegant and reusable; it's the code that runs a million times every second that needs to be fast.



Another point, of course, is that recursion is kinda hard to do without splitting the code up into methods. Trying to put your whole program into one method AND making that method recursive will probably slow the program down or even crash it.
deonejuan
2013-05-02 13:05:59 UTC
Depends on the calls to the CPU written into the code. If the method in question is recursive then yes, you will slow things down because all the loops have to be assigned in memory before the method returns.



method()s are called sub-routines in other languages. We code method() to make program coding easier. method()s add behavior, in Java's view of the world, to Objects. Non-optimized code is code that is constantly configuring another piece of memory.
Almighty Wizard
2013-05-02 12:11:58 UTC
Depends on how you use them.



Typically, the way to have your application perform fastest is to NOT use methods, just because calling a method has additional overhead associated with the execution. However, that would only work for simple applications. Complex applications almost always require using additional methods here and there. In the grand scheme of things, as long as you aren't doing something terribly inefficient, processors work fast enough that you won't notice any differences in execution time.



In short, you really shouldn't worry about it unless you are required to increase performance at a nanosecond scale.
2016-08-09 05:07:20 UTC
I'd go along with A. Randomly sticking code in a approach is not going to make it faster. Do it incorrect, and matters might be slower, primarily if the process known as is a ways sufficient away in RAM to be beyond the size of the cache (inflicting a miss), plus the overhead of a name instruction.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...