All Questions
Tagged with performance c++
6,001
questions
2
votes
1
answer
55
views
Run time depends on code after the measured part
Consider the following C++23 program (online).
#include <chrono>
#include <cstdio>
#include <print>
constexpr size_t NWrites = 10000000000;
#define DEST_STORAGE static
#define ...
0
votes
3
answers
87
views
nested switch statements for template arguments
We have some templated function that takes 2 compile-time arguments, e.g.
template<int a, int b>
void SomeFunc(double *x, double *y, double *z);
For performance reasons, it is better to have ...
0
votes
0
answers
23
views
Enabling Efficiency Mode (EcoQoS) for a Process by PID in C++ and Verifying in Task Manager
I'm trying to enable Efficiency Mode (EcoQoS) for a process given its process ID (PID). I have referred to this similar question on Stack Overflow, but I'm still facing issues.
Issue:
Although the ...
1
vote
0
answers
50
views
How to Align Memory to Cache Line Using Boost Interprocess Shared Memory
I am writing a Boost.Interprocess code for shared memory between two processes. I am using managed_shared_memory. I'm initializing the memory using boost::interprocess::open_or_create, and I am ...
1
vote
1
answer
61
views
why the loop order influence the efficiency of code in GEMM?
I'm writing a GEMM code in C++, but the loop order actually influences a lot to the efficiency. Code:
// The first edition
void gemm_cpu_naive(
int* __restrict__ C,
const int* __restrict__ A, ...
1
vote
1
answer
77
views
Exposing what the compiler does for passing to a reference vs to a const reference
Compiling the code shown below, and examining the disassembly (objdump -DSs a.out > a.dis), I am unable to see any difference in what happens for invoking fun_ref(T &) and fun_const_ref(const T ...
1
vote
0
answers
52
views
How to reduce cache miss in SPSC queue pop function?
I am working on optimizing a Single Producer Single Consumer (SPSC) queue in C++. The following is the miniman reproducible example for my implementation:
#include <atomic>
#include <cstddef&...
0
votes
2
answers
170
views
Are bitwise operators slower than common loops like a for loop?
Do bitwise operators like & (AND) take more runtime than common for loops?
Today I was doing a power of 2 question in LeetCode. My code was this:
if (n > 0 && (n & (n - 1)) == 0) {
...
0
votes
0
answers
46
views
How to performantly submit messages of arbitrary size (and fields) to a common interface?
I have a message processing interface that accepts messages of different types. What I have now goes along the lines of this:
Demo
#include <iostream>
enum class MsgType
{
write,
read,
...
0
votes
0
answers
77
views
How to properly read over TCP when size is not known?
I've always used "over TCP" protocols with a fixed and known size. But what about reading data without a known size where the 'end' is marked by a delimiter? (e.g HTTP Headers) This is the ...
1
vote
1
answer
61
views
Low Latency Communication in C++: Raw Sockets with io_uring vs. ZeroMQ, NanoMQ, Aeron etc [closed]
I have two C++ services that need ultra low-latency communication over the network. I'm considering using raw sockets with io_uring but also looking at possibilities of using various messaging queues ...
1
vote
0
answers
118
views
Why doesn't this SIMD code show better performance?
I wrote a simple code to test the performance and set my data:
#include <iostream>
#include <ctime>
#include <smmintrin.h>
#include <immintrin.h>
unsigned t0, t1;
struct VAL
...
-2
votes
2
answers
114
views
Possible Improvements to sum(int n) function or calling code in C++
I have a function that calculates sum of the numbers up to a number N and returns it back to calling code as follows:
long sumUpToNumber(int n){
long sum;
return sum = n * (n + 1) / 2;
}
int ...
2
votes
1
answer
213
views
Is bitwise shift more "efficient" than a for loop? [closed]
I just started going through "Data Structures and Algorithms in C++" and in one of the intro exercises they ask to write a function which returns powers of 2 and takes the exponent n
as an ...
5
votes
3
answers
397
views
The most efficient way to test if a positive integer is 2^n (i.e. 1, 2, 4, 8, etc.) in C++20?
A handy method to verify if a positive integer n is a power of two (like 1, 2, 4, 8, etc.) is to use the following test for having no more than 1 bit set:
bool test = n & (n - 1) == 0;
This ...