Questions tagged as 'desempenho'

1
answer

Why is using String in a switch block more efficient than in an if-else block?

According to Java documentation : / p>    The Java compiler generally generates more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.     
asked by 26.08.2015 / 16:37
3
answers

___ ___ erkimt Performance: string "concatenated" or all on the same line? ______ qstntxt ___

I have a loop with thousands of results that renders a table. The string is mounted through the StringBuilder in this way (example snippet):

%pre%

Can there be any performance improvement if I put the whole code in one line, as below?

%pre%

And if it were a concatenation, such as the following code?

%pre%

More advanced explanations of how language works are welcome.

    
______ azszpr7738 ___

The best will always be to keep in a String that is unique at compile time rather than at runtime.

And, believe it or not, in C # you can use line breaks in a single String using the character %code% (at) at the beginning of it.

%pre%

Also, according to this topic in SO , if you concatenate String literals in a same command, they will be concatenated at compile time, then it would give the same effect on runtime performance something like:

%pre%

Using %code% to concatenate String literals will decrease program performance, since it will prevent the compiler from optimizing literals.

On the other hand, if the compiler can not know the size of the String at compile time, for example if we concatenate variables, %code% is usually faster than directly concatenating the values. I say "generally" because I do not really know how much the compiler or CPU would be able to optimize certain exceptional cases.

    
______ azszpr7751 ___

In case of substituting multiple calls from %code% to only one, the gain will be minimal.

On one occasion, working with the class %code% , I noticed a great improvement in performance when booting the %code% with a size sufficient to store the entire result. I did this using two routines, one to count the required size, and another to build the string.

To initialize the StringBuilder with a size simply pass an integer in the con- tractor:

%pre%     
______ azszpr21973 ___

It has been properly said that the best way is to use a single %code% in the case presented. It's fast and legible.

But if there is a reason to do sequential concatenation, it can be used without performance problems. It ends up becoming a single %code% at compile time. The only problem is being less readable.

There is optimization of the compiler in many cases. But not at all. If all of the% s sizes involved are not known, there is not so much optimization. In these cases the optimization only transforms the concatenations in the %code% method. It is certainly better because it avoids unnecessary allocations, but the size calculation is still needed.

Although the %code% property is of type %code% , I have doubts if it can be so optimized.

If it is useful a %code% may be appropriate and not have a performance impairment. It may even be faster than a method %code% of %code% . When you know the total size required for all% s of% s in a simple way, possibly as a constant literal, %code% is very fast. Internally %code% uses a %code% so you find that the former is faster that the second one does not make sense. Of course, in cases that have only 4% with% s the concatenation is made simpler without %code% .

But so far little has been added to what has been said in the other answers.

Alternative: Resources

There are cases to make work easier, the text should not be in the code. It should be in an external file, when this is pertinent, or be in Resources . It may be easier to maintain in this and it is very fast in the second case.

For everything there is the best solution applied. This is a case that easier maintenance can be more important than performance. In case of catching the resource the performance will not be much affected either. And even if it is, it will not create problem for the application. This performance concern makes sense for cases of extreme strings manipulations . In the case of file access the performance will obviously be affected by access to mass memory. But it will still not affect the application. Of course this solution should only be chosen when you want to be able to change the text easily after compilation, when you want to give this freedom to the user.

    
___

I have a loop with thousands of results that renders a table. The string is mounted through the StringBuilder in this way (example snippet): ScriptTbody.AppendLine("</select>"); ScriptTbody.AppendLine("<p class=\"valAnoInicio\"><...
asked by 28.02.2014 / 13:21
2
answers

Is it safe to minify HTML?

Like JS and CSS, HTML can also be "minified": <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="wi...
asked by 10.07.2014 / 18:47
4
answers

Is method overload less performative?

I was reading about interfaces as part of my studies and came across an overloading class of methods, with two comments saying that this should be avoided. I was in doubt about it affecting the performance. Affect?     
asked by 21.10.2017 / 06:15
4
answers

What ways to measure the performance of an algorithm?

If I have, for example, some sort algorithms (Merge sort, Quick sort, Bubble sort ...) in which way (s) can I know the efficiency of each?     
asked by 17.11.2015 / 14:34
2
answers

Why invert the index of the array in a for, to popular the array, makes the process faster?

I was doing some tests in Java and noticed a very strange behavior. I made a two-dimensional array and populated the array in two different ways, filling in random numbers up to 2 ^ 31-1 and just changing its index. I got a considerable performa...
asked by 13.03.2014 / 04:09
3
answers

How to optimize this function for Fibonacci sequence?

On codility site there is an initial challenge for you to refactor this code: var yourself = { fibonacci : function(n) { if (n === 0) { return 0; } if (n === 1) { return 1; }...
asked by 13.01.2017 / 00:24
1
answer

What is the advantage of a semicolon in programming languages?

I know that in programming languages like Java and C #, the semicolon indicates the end of the statement, as in the example below, in C #: System.Console.WriteLine("Hello, World!"); However, there are still languages in which the semicolon...
asked by 27.02.2017 / 01:16
4
answers

How to limit the resources used by the system?

I have a loop of repetition that does several iterations and has in its scope calculations that require a lot of processing. The problem is that when running the code snippet, processor usage will gradually rise, until the application crashes...
asked by 24.11.2015 / 18:24
1
answer

What makes Kotlin a faster language than Java?

I've read in some articles that Kotlin is faster than Java, but none of them exemplifies why. [...] As fast as Java ". Kotlin - Evolve your Java code (TDC-2016) Alex Magalhaes [...] Kotlin must compile quickly, coexist with Java and...
asked by 08.09.2017 / 14:13