C
C
Chvalov2015-09-19 18:33:53
Java
Chvalov, 2015-09-19 18:33:53

How to understand - to optimize the code?

Guys, I'm completely confused, explain how to understand the optimized code.
If possible, give examples of code and the same code only optimized.
And another question, if there is a variable in which a number greater than 100 is not written (let's say the load line there is from 0 to 100) and if you change the type int to byte, this is already considered an optimization (It seems like much less memory will be allocated)?
And another optimization, in terms of - fewer lines of code but more difficult to execute or more code but easier on the machine?
And a bit off topic: Does code refactoring also include code optimization or just reworking the code for a more understandable visual perception?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
T
Tiberal, 2015-09-20
@Chvalov

The simplest example of optimization

String output = "Some text"; 
    int count = 100; 
    for(int i =0; i<count; i++) { 
    output += i; 
    } 
    return output;

against
StringBuffer output = new StringBuffer(110); 
    output.append("Some text"); 
    for(int i =0; i<count; i++) { 
        output.append(i); 
    } 
    return output.toString();

The code does the same thing, but in the first case, when output += i is called, a new line will be created each time, in the second we use one object. Thus, in the first case, 101 lines will hang out in memory; in the second case, one object from which we can pull out a line.

I
IceJOKER, 2015-09-19
@IceJOKER

gmvezjNt0Kq7ra.png

�
âš¡ Kotobotov âš¡, 2015-09-19
@angrySCV

byte in java also compiles to int, meaning changing the data type changes absolutely nothing.
and just do optimization is usually done in approaches, in concepts, in classes of algorithms, and changing the data type is not optimization - it is ananism.

A
angry_cellophane, 2015-09-19
@angry_cellophane

Usually, optimization is understood as:
1. Acceleration of program execution.
2. Reducing the memory used.
Example: to work with xml in Java, it is fashionable to use JAXP and SAX parsers. SAX is a one-pass parser that does not load entire xml files into memory, but suggests using something like callbacks on certain events (events: tag start, tag end, etc.). Thus, it saves memory, but if you need to work with the DOM structure of the xml file, then the sax parser will have to go through the document several times, which increases the work time. For this reason, there is a JAXP parser that first parses the entire xml document, builds the DOM for it, but requires much more memory, but the work time is significantly reduced. As you can see, the optimization of memory usage / runtime are interrelated, therefore, before optimization, it is worth learning as much as possible about non-functional requirements for the program (Non functional requirements - NFRs),
Refactoring does not imply optimization. Refactoring is primarily about improving the readability, maintainability, and extensibility of code. A spherical example in a vacuum: at the beginning of the project, it was decided to directly instantiate objects of the Bird class, then the realization came that it was better to use the factory method, because for some places for tests you need to use Dummy objects. Then we decided to move on to factories, because now our system must distinguish between Duck and Goose. These changes are not related to non-functional requirements.

A
Alejandro Esquire, 2015-09-19
@A1ejandro

The same task was given by the pros and the schoolboy. Everyone wrote a solution (his own code). The student's solution consists of 1000 lines of code, the program runs for 1 hour and gives the correct result.
The pro's solution consists of 250 lines of code, the program runs for 1 minute and also gives the correct result. Which of the codes is optimal (optimized), and which is not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question