How are we doing? Please help us improve Stack Overflow. Take our short survey

Questions tagged [micro-optimization]

Micro-optimization is the process of meticulous tuning of small sections of code in order to address a perceived deficiency in some aspect of its operation (excessive memory usage, poor performance, etc).

micro-optimization
Filter by
Sorted by
Tagged with
4 votes
2 answers
662 views

Efficient UTF-8 character-length decoding for a non-zero character in a 32 bit register

I'm storing a UTF-8 character in eax and later on, in processing, I need to know how many bytes make up the character. I've narrowed down going about this that minimizes shifts and masks and wanted ...
Frank C.'s user avatar
  • 7,970
9 votes
3 answers
17k views

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

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 ...
Zeyad Etman's user avatar
  • 2,420
9 votes
3 answers
1k views

Which integer operations have higher performance alternate methods in Rust?

When writing integer functions in Rust which will run millions of times (think pixel processing), it's useful to use operations with the highest performance - similar to C/C++. While the reference ...
ideasman42's user avatar
  • 45.3k
1 vote
1 answer
427 views

php - performance of continue state

I was thinking today at work: is there a much faster way to get results using continue state? for ($i=0; $i<5000; $i++) { if (!($i % 2)) { continue; } do_something_odd($i); } ...
Ivijan Stefan Stipić's user avatar
5 votes
1 answer
7k views

latency vs throughput in intel intrinsics

I think I have a decent understanding of the difference between latency and throughput, in general. However, the implications of latency on instruction throughput are unclear to me for Intel ...
Jimbo's user avatar
  • 3,106
3 votes
2 answers
285 views

Is it more efficient to multiply within the address displacement or outside it?

I'm writing an x86 assembly routine that takes as arguments: At [ESP+4]: The number of 32-bit integers following. Starting at [ESP+8]: A list of 32-bit integers to add up. And it returns the sum of ...
Govind Parmar's user avatar
2 votes
3 answers
3k views

Multiplication with constant - imul or shl-add-combination

This question is about how we multiply an integer with a constant. So let's look at a simple function: int f(int x) { return 10*x; } How can that function be optimized best, especially when ...
overseas's user avatar
  • 1,711
8 votes
1 answer
512 views

May there be any penalties when using 64/32-bit registers in Long mode?

Probably this is all about not even micro- but nanooptimizations, but the subject interests me and I would like to know if there are any penalties when using non-native register sizes in long mode? I'...
Alexander Zhak's user avatar
4 votes
4 answers
548 views

Does optimizing code in TI-BASIC actually make a difference?

I know in TI-BASIC, the convention is to optimize obsessively and to save as many bits as possible (which is pretty fun, I admit). For example, DelVar Z Prompt X If X=0 Then Disp "X is zero" End ...
user3932000's user avatar
1 vote
5 answers
207 views

What is the most efficient way to convert space-delimited numbers to an array of integers?

My problem is simple: I have a real (not imagined) performance bug in trying to do conversions like "1273 912 84" --> {1273, 912, 84} and so I want to figure out how to do it as fast as possible. ...
Deadly Nicotine's user avatar
4 votes
1 answer
1k views

repz ret: why all the hassle?

The issue of the repz ret has been covered here [1] as well as in other sources [2, 3] quite satisfactorily. However, reading neither of these sources, I found answers to the following: What is the ...
The Vee's user avatar
  • 11.5k
20 votes
8 answers
25k views

Very fast approximate Logarithm (natural log) function in C++?

We find various tricks to replace std::sqrt (Timing Square Root) and some for std::exp (Using Faster Exponential Approximation) , but I find nothing to replace std::log. It's part of loops in my ...
qwark's user avatar
  • 493
2 votes
1 answer
1k views

Cost of a 64bits jump, always 10-22 cycles the first time?

In x86_64 there is no direct jump with a 64 bits address. Only a 32 bits one. With indirect jumps I understand the pipeline HAS TO BE RESOLVED ONCE before branch prediction comes into play. My ...
Simon's user avatar
  • 2,096
1 vote
1 answer
510 views

How to keep input-dependent hot data in registers when using SIMD intrinsics

I am trying to use Intel SIMD intrinsics to accelerate a query-answer program. Suppose query_cnt is input dependent but is always smaller than SIMD register count (i.e. there is enough SIMD registers ...
MarZzz's user avatar
  • 339
35 votes
3 answers
5k views

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

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 ...
BeeOnRope's user avatar
  • 62.5k
1 vote
1 answer
63 views

Should I use .select or .each to sort array of hashes into 3 separate arrays? (Ruby)

I have a single array of hashes that I want to sort into 3 separate arrays based on a value. There are two obvious ways to do this. arr1 = orig_array.select { |h| h[:category] == 1 } arr2 = ...
nothing's user avatar
  • 25
7 votes
0 answers
2k views

ISO C replacement for GCC's computed gotos

Background: In an interpreter loop (or other finite state machine) there are a couple ways to dispatch operations (or jump state based on the next input). The obvious way in C is with a switch ...
ktb's user avatar
  • 1,527
-3 votes
3 answers
229 views

C++ code optimization using expressions instead of variables [duplicate]

I have a question about creating optimal C++ program. I have a function that computes expressions like: c= a/2 c = (a*b)/2 c = (a/2) + b etc. is it better to use a variable to store these values ...
weu833's user avatar
  • 1
4 votes
1 answer
645 views

Can two instructions execute in the same cycle if the same register is used as input and output respectively?

For example, could these two instructions begin executing in the same cycle or do they interfere with each other? MOV %RAX, (ADDR) # AT&T syntax: store rax POP %RAX
Eloff's user avatar
  • 21.3k
-5 votes
1 answer
91 views

Most Efficient Way to Compare 2 Primitives in Java?

Which code is better or optimized or efficient? double a; double b; if (a == b) return true; or if (a - b == 0) return true;
Mahmud Riad's user avatar
  • 1,169
3 votes
1 answer
3k views

Does each Floating point operation take the same time?

I believe integer addition or subtraction always take the same time no matter how big the operands are. Time needed for ALU output to be stabilized may vary over input operands, but CPU component that ...
codebomb's user avatar
  • 260
4 votes
1 answer
1k views

Is the fall-through side of a conditional branch more efficient? Is it a good idea to make that the error-handling side?

Consider a function that calls another function and checks for error. Assume that the function CheckError() returns 0 on failure, and other numbers indicates success. First version: taken branch for ...
J.Smith's user avatar
  • 111
0 votes
1 answer
109 views

Optimizing upload: request sent

I'm working on an async uploader in JavaScript intended to send multi-gigabyte files to our server efficiently. It uses JavaScript's FileReader to slice the files 5MB chunks at a time, and send 5 ...
rodrigo-silveira's user avatar
6 votes
2 answers
1k views

Do complex addressing modes have extra overhead for loads from memory?

Is there a difference in performance between these mov load instructions? Do the more complex addressing modes have extra overhead (latency or throughput) compared to the simple ones? # AT&T ...
mosceo's user avatar
  • 1,222
0 votes
1 answer
781 views

x86 - Instruction-level parallelism - optimal order of instructions

Which of the following two snippets of x86_64 code should be fastest? Or no difference at all? ; #1 bsf rax, rdi mov rdx, -1 cmove rax, rdx vs. ; #2 mov rdx, -1 bsf ...
Cecil Ward's user avatar
6 votes
4 answers
652 views

Threshold an absolute value

I have the following function: char f1( int a, unsigned b ) { return abs(a) <= b; } For execution speed, I want to rewrite it as follows: char f2( int a, unsigned b ) { return (unsigned)(a+b) &...
Brent Bradburn's user avatar
0 votes
1 answer
117 views

How to maximize performance of an asynchronous scheduler?

I've noticed a strange technique in JavaScript that I've used to increase performance in repetitive processes that paint on a canvas. In the future, I plan to use a SharedBufffer or even a ...
Patrick Roberts's user avatar
7 votes
2 answers
4k views

C# lambda allocation and collection

I saw this question today about some performance difference regarding ConcurrentDictionary methods, and I saw it as a premature micro-optimization. However, upon some thought, I realized (if I am not ...
Lou's user avatar
  • 4,344
4 votes
1 answer
841 views

Avoiding Translation Look aside buffer ( TLB ) pollution when mmap()

When we want to write a data item, the block containing the data is brought into the cache first and data item is written into the cache. This can cause cache pollution. To avoid this, Intel has ...
KodeWarrior's user avatar
  • 3,558
1 vote
2 answers
98 views

Checking a conditional vs setting a variable multiple times; low-level optimization

If I'm searching through a collection of values and running code for each one, and I want to turn a boolean on when I find a certain quality and then back off again when I've run the code for that ...
c..'s user avatar
  • 1,044
0 votes
1 answer
359 views

What is the minimal number of dependency chains to maximize the execution throughput?

Given a chain of instructions linked by true dependencies and repeated periodically (i.e. a loop), for example (a->b->c)->(a->b->c)->... Assuming that it can be split into several shorter and ...
DMH's user avatar
  • 3
15 votes
2 answers
4k views

The advantages of using 32bit registers/instructions in x86-64

Sometimes gcc uses 32bit register, when I would expect it to use a 64bit register. For example the following C code: unsigned long long div(unsigned long long a, unsigned long long b){ return a/...
ead's user avatar
  • 33.7k
3 votes
3 answers
238 views

Optimization of a short-length cyclic convolution

I have two sequences of 8 unsigned bytes and I need to compute their cyclic convolution, which yields 8 unsigned 19 bits integers. As I repeat this million times, I want to optimize. The ...
user avatar
3 votes
4 answers
500 views

How performance heavy is String concatenation?

Following situation: I have a lot of log statements which should be only exectuted when in Debug mode. I have two options on how to implement this: A public void log(String message){ if(debug) ...
F43nd1r's user avatar
  • 7,720
3 votes
2 answers
700 views

In x86 assembly, is it better to use two separate registers for imul?

I am wondering, mostly out of curiosity, if using the same register for an operation is better than using two. What would be better, considering performance and/or other concerns? mov %rbx, %rcx imul ...
user2251284's user avatar
-1 votes
1 answer
125 views

Optimizing per-thread copying and 0-padding of data in registers/L1

I'm writing a kernel which, among other things, has each thread populate one variable with data constituting the lower bytes, and pads the rest (assuming little-endianness). This is done repeatedly ...
einpoklum's user avatar
  • 124k
0 votes
2 answers
462 views

C++ - Are const parameters and class variables pessimization?

I'm trying to figure out when const should be used when writing C++ code. Are these all examples of pessimization or is it beneficial to write code this way?: Example 1: int findVal(const int ...
Matthew D. Scholefield's user avatar
2 votes
2 answers
184 views

Can I avoid cache consistency checks by declaring variables as thread-local?

I'm reading about how CPUs maintain the consistency of their caches in a multithreaded application. A write in one core's cache labels it as dirty and all other cores must be careful to not read that ...
Jim Pivarski's user avatar
  • 5,766
2 votes
3 answers
193 views

Which Boolean is Faster? < or <=

I'm doing some work involving processing an insane amount of data in browser. As a result I'm trying to optimize everything down to the nuts and bolts. I don't need anyone telling me that I'm wasting ...
Joe Thomas's user avatar
  • 6,087
4 votes
2 answers
1k views

According to Intel my cache should be 24-way associative though its 12-way, how is that?

According to “Intel 64 and IA-32 architectures optimization reference manual,” April 2012 page 2-23 The physical addresses of data kept in the LLC data arrays are distributed among the cache ...
alex10791's user avatar
  • 454
2 votes
1 answer
465 views

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

I am analyzing an example of a loop from Agner Fog's optimization_assembly. I mean the 12.9 chapter. The code is: ( I simplified a bit) L1: vmulpd ymm1, ymm2, [rsi+rax] vaddpd ymm1, ymm1, [...
user avatar
4 votes
1 answer
82 views

Unexpected slowdown from inserting a nop in a loop, and from reading near a movnti store

I cannot understand why the first code has ~1 cycle per iteration and second has 2 cycle per iteration. I measured with Agner's tool and perf. According to IACA it should take 1 cycle, from my ...
user avatar
3 votes
2 answers
451 views

Why isn't MOVNTI slower, in a loop storing repeatedly to the same address?

section .text %define n 100000 _start: xor rcx, rcx jmp .cond .begin: movnti [array], eax .cond: add rcx, 1 cmp rcx, n jl .begin section .data array times 81920 db "A&...
user avatar
1 vote
1 answer
62 views

What exactly is low bits subtraction

I was reading this article and thought that that everything was perfectly clear until I stumble upon this: Again, most real Scheme systems use a slightly different implementation; for example, if ...
P. P's user avatar
  • 21
2 votes
1 answer
193 views

Latency of short loop

I'm trying to understand why some simple loops run at the speeds they do First case: L1: add rax, rcx # (1) add rcx, 1 # (2) cmp rcx, 4096 # (3) jl L1 And according to IACA, ...
Gilgamesz's user avatar
  • 4,883
0 votes
1 answer
349 views

x86 decoding of multi-uop instructions

Agner Fog in his microarch.pdf says: Decoding becomes more efficient because an instruction that generates one fused μop can go into any of the three decoders while an instruction that ...
Gilgamesz's user avatar
  • 4,883
1 vote
1 answer
2k views

x86-64 Relative jmp performance

I'm currently doing an assignment that measures the performance of various x86-64 commands (at&t syntax). The command I'm somewhat confused on is the "unconditional jmp" command. This is how I've ...
acz's user avatar
  • 97
2 votes
1 answer
2k views

Dependency chain analysis

From Agner Fog's "Optimizing Assembly" guide, Section 12.7: a loop example. One of the paragraphs discussing the example code: [...] Analysis for Pentium M: ... 13 uops at 3 per clock = one ...
Gilgamesz's user avatar
  • 4,883
1 vote
1 answer
1k views

The most efficient way of counting positive, negative and zero number using loop unrolling

Say I have the following instruction, simply checks if a number is positive or not (negative or zero) and if it was positive add 1 to our counter (and we don't care if the numbers is negative or zero)....
Yar's user avatar
  • 7,238
1 vote
1 answer
891 views

Predecoders and decoders. Difference

I am reading Agner Fog's materials and I have some doubts: The pre-decoders and decoders can handle 16 bytes or 4 instructions per clock cycle What is pre-decoders in context of decoders? The ...
Gilgamesz's user avatar
  • 4,883

1
9 10
11
12 13
19