micro-optimization

Java - Declaring variables in for loops

浪子不回头ぞ 提交于 2019-12-17 19:12:45
问题 Is declaring a variable inside of a loop poor practice? It would seem to me that doing so, as seen in the first code block below, would use ten times the memory as the second... due to creating a new string in each iteration of the loop. Is this correct? for (int i = 0; i < 10; i++) { String str = "Some string"; } vs. String str; for (int i = 0; i < 10; i++) { str = "Some String"; } 回答1: Is declaring a variable inside of a loop poor practice? Not at all! It localizes the variable to its point

What's the easiest way to determine if a register's value is equal to zero or not?

☆樱花仙子☆ 提交于 2019-12-17 16:47:11
问题 I'm using x86 assembly with the Irvine library. What's the easiest way to check if a register value is equal to zero or not? I used cmp instruction but i'm searching for alternative way. This is my code using cmp instruction and the register is ebx cmp ebx,0 je equ1 mov ebx,0 jmp cont equ1: mov ebx,1 jmp cont cont: exit This "booleanizes" a value, producing a 0 or 1 like int ebx = !!ebx would in C. 回答1: Probably the "easiest", or simplest, "not-caring about details" answer how to determine is

array_push() vs. $array[] = … Which is fastest? [duplicate]

随声附和 提交于 2019-12-17 10:57:50
问题 This question already has answers here : What's better to use in PHP $array[] = $value or array_push($array, $value)? (10 answers) Closed 4 years ago . I need to add values received from MySQL into an array [PHP], here is what I've got: $players = array(); while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) { $players[] = $homePlayerRow['player_id']; } Is this the only way of doing it? Also , is the following faster/better? $players = array(); while ($homePlayerRow = mysql_fetch

array_push() vs. $array[] = … Which is fastest? [duplicate]

不羁的心 提交于 2019-12-17 10:57:37
问题 This question already has answers here : What's better to use in PHP $array[] = $value or array_push($array, $value)? (10 answers) Closed 4 years ago . I need to add values received from MySQL into an array [PHP], here is what I've got: $players = array(); while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) { $players[] = $homePlayerRow['player_id']; } Is this the only way of doing it? Also , is the following faster/better? $players = array(); while ($homePlayerRow = mysql_fetch

Is performance reduced when executing loops whose uop count is not a multiple of processor width?

▼魔方 西西 提交于 2019-12-16 20:57:49
问题 I'm wondering how loops of various sizes perform on recent x86 processors, as a function of number of uops. Here's a quote from Peter Cordes who raised the issue of non-multiple-of-4 counts in another question: I also found that the uop bandwidth out of the loop buffer isn't a constant 4 per cycle, if the loop isn't a multiple of 4 uops. (i.e. it's abc, abc, ...; not abca, bcab, ...). Agner Fog's microarch doc unfortunately wasn't clear on this limitation of the loop buffer. The issue is

Loop optimization. How does register renaming break dependencies? What is execution port capacity?

房东的猫 提交于 2019-12-13 16:23:41
问题 I am analyzing a example of loop from Agner Fog's optimization_assembly. I mean the 12.9 chapter. The code is: ( I simplify a bit) L1: vmulpd ymm1, ymm2, [rsi+rax] vaddpd ymm1, ymm1, [rdi+rax] vmovupd [rdi+rax], ymm1 add rax, 32 jl L1 And I have some questions: The author said that there is no loop-carried depndency. I don't understand why there is no. ( I skip case of add rax, 32 ( it is loop-carried indeed, but only one cycle)). But, after all, the next iteration cannot modify ymm1 register

C# How do basic operation time vary with the size of the numbers?

戏子无情 提交于 2019-12-13 10:29:16
问题 Context of this is a function, which needs to run pretty much once per frame, and is therefore very critical performance-wise. This function contains a loop, and operations inside it. private int MyFunction(int number) { // Code for (int i = 0; i <= 10000; i++) { var value = i * number var valuePow2 = value * value; // Some code which uses valuePow2 several times } return 0; // Not actual line } Now, because of mathematical properties, we know that (a * b)² is equal to a² * b² So, it would be

JS Private methods not redefined at each constructor call

廉价感情. 提交于 2019-12-12 17:31:58
问题 How do you make a Javascript private method that is not redefined each time you call the constructor ? As far as I know, in OOP-JS, private methods are methods defined in the "constructor method" of one's "class", called each time one instantiates a new "object". I was thinking maybe a function declaration (i.e. function name() , as opposed to function expression var name = function() ) would do the trick, but how can I be sure that the following code only declares my function once ?

Make a register depend on another one without changing its value

只愿长相守 提交于 2019-12-12 15:07:11
问题 Consider the following x86 assembly: ; something that sets rax mov rcx, [rdi] xor rax, rcx xor rax, rcx At the end of the sequence, rax has the same value as it had on entry, but from the point of view of the CPU its value depends on the value loaded from memory into rcx . In particular, subsequent use of rax won't start until that load and the two xor instructions complete. Is there any way to achieve this effect more efficiently than the two- xor sequence, e.g., with a single one-uop one

Any possible code that can flip a bit/integer/bool between 0 and 1 in single CPU instruction

柔情痞子 提交于 2019-12-12 13:09:34
问题 Can a single x86 instruction toggle a boolean value between '0' and '1'? I thought of following ways but all result in two instruction with -O3 flag of gcc. status =! status; status = 1 - status; status = status == 0 ? 1: 0; int flip[2] = {1, 0}; status = flip[status]; Is there a faster way to do this? This is what I tried: https://godbolt.org/g/A3qNUw What I need is a function which toggles the input and returns, written in a way that compiles to one instruction. Something similar to this