optimization

Do compilers automatically optimise repeated calls to mathematical functions?

…衆ロ難τιáo~ 提交于 2020-01-12 12:08:26
问题 Say I had this snippet of code: #include <cmath> // ... float f = rand(); std::cout << sin(f) << " " << sin(f); As sin(f) is a well defined function there is an easy optimisation: float f = rand(); float sin_f = sin(f); std::cout << sin_f << " " << sin_f; Is this an optimisation that it's reasonable to expect a modern C++ compiler to do by itself? Or is there no way for the compiler to determine that sin(f) should always return the same value for an equal value of f ? 回答1: Using g++ built

Quicksort optimizations

穿精又带淫゛_ 提交于 2020-01-12 09:39:52
问题 I'm learning sorting algorithms and as next step, I'm trying to get my implementation perform close to the std::sort() . I'm pretty far, so far.. :-) I have 3 implementations of quicksort: standard quicksort (using temp arrays). quicksort with following optimizations: median3 used to select median tail-recursion quicksort applied only upto partition sizes < 16. For smaller partitions insertion sort is used. insertion sort applied to the whole array at once instead of applying to each

Quicksort optimizations

匆匆过客 提交于 2020-01-12 09:39:04
问题 I'm learning sorting algorithms and as next step, I'm trying to get my implementation perform close to the std::sort() . I'm pretty far, so far.. :-) I have 3 implementations of quicksort: standard quicksort (using temp arrays). quicksort with following optimizations: median3 used to select median tail-recursion quicksort applied only upto partition sizes < 16. For smaller partitions insertion sort is used. insertion sort applied to the whole array at once instead of applying to each

Why does the speed of this SOR solver depend on the input?

久未见 提交于 2020-01-12 08:19:43
问题 Related to my other question, I have now modified the sparse matrix solver to use the SOR (Successive Over-Relaxation) method. The code is now as follows: void SORSolver::step() { float const omega = 1.0f; float const *b = &d_b(1, 1), *w = &d_w(1, 1), *e = &d_e(1, 1), *s = &d_s(1, 1), *n = &d_n(1, 1), *xw = &d_x(0, 1), *xe = &d_x(2, 1), *xs = &d_x(1, 0), *xn = &d_x(1, 2); float *xc = &d_x(1, 1); for (size_t y = 1; y < d_ny - 1; ++y) { for (size_t x = 1; x < d_nx - 1; ++x) { float diff = *b -

Why does the speed of this SOR solver depend on the input?

☆樱花仙子☆ 提交于 2020-01-12 08:16:35
问题 Related to my other question, I have now modified the sparse matrix solver to use the SOR (Successive Over-Relaxation) method. The code is now as follows: void SORSolver::step() { float const omega = 1.0f; float const *b = &d_b(1, 1), *w = &d_w(1, 1), *e = &d_e(1, 1), *s = &d_s(1, 1), *n = &d_n(1, 1), *xw = &d_x(0, 1), *xe = &d_x(2, 1), *xs = &d_x(1, 0), *xn = &d_x(1, 2); float *xc = &d_x(1, 1); for (size_t y = 1; y < d_ny - 1; ++y) { for (size_t x = 1; x < d_nx - 1; ++x) { float diff = *b -

Best infinite loop [duplicate]

妖精的绣舞 提交于 2020-01-12 08:10:22
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: while (1) Vs. for (;;) Is there a speed difference? Hi, Which is better,faster and more optimized way to implement infinite loop - for(;;) or while(1)? and why? 回答1: In any normal compiler, there should be absolutely no difference. For example, here's what LLVM-clang generates (with the -O3 flag) for while (1) {} : .file "test.c" .text .globl main .align 16, 0x90 .type main,@function main: pushl %ebp movl %esp,

Efficiently draw a grid in Windows Forms

断了今生、忘了曾经 提交于 2020-01-12 07:55:43
问题 I'm writing an implementation of Conway's Game of Life in C#. This is the code I'm using to draw the grid, it's in my panel_Paint event. g is the graphics context. for (int y = 0; y < numOfCells * cellSize; y += cellSize) { for (int x = 0; x < numOfCells * cellSize; x += cellSize) { g.DrawLine(p, x, 0, x, y + numOfCells * cellSize); g.DrawLine(p, 0, x, y + size * drawnGrid, x); } } When I run my program, it is unresponsive until it finishes drawing the grid, which takes a few seconds at

Optimize MATLAB code (nested for loop to compute similarity matrix)

浪子不回头ぞ 提交于 2020-01-12 06:23:55
问题 I am computing a similarity matrix based on Euclidean distance in MATLAB. My code is as follows: for i=1:N % M,N is the size of the matrix x for whose elements I am computing similarity matrix for j=1:N D(i,j) = sqrt(sum(x(:,i)-x(:,j)).^2)); % D is the similarity matrix end end Can any help with optimizing this = reducing the for loops as my matrix x is of dimension 256x30000 . Thanks a lot! --Aditya 回答1: The function to do so in matlab is called pdist. Unfortunately it is painfully slow and

Optimize MATLAB code (nested for loop to compute similarity matrix)

不羁岁月 提交于 2020-01-12 06:23:11
问题 I am computing a similarity matrix based on Euclidean distance in MATLAB. My code is as follows: for i=1:N % M,N is the size of the matrix x for whose elements I am computing similarity matrix for j=1:N D(i,j) = sqrt(sum(x(:,i)-x(:,j)).^2)); % D is the similarity matrix end end Can any help with optimizing this = reducing the for loops as my matrix x is of dimension 256x30000 . Thanks a lot! --Aditya 回答1: The function to do so in matlab is called pdist. Unfortunately it is painfully slow and

Faster I/O in C

自闭症网瘾萝莉.ら 提交于 2020-01-12 05:30:14
问题 I have a problem which will take 1000000 lines of inputs like below from console. 0 1 23 4 5 1 3 5 2 56 12 2 3 33 5 ... ... I have used scanf, but it is very very slow. Is there anyway to get the input from console in a faster way? I could use read(), but I am not sure about the no of bytes in each line, so I can not as read() to read 'n' bytes. Thanks, Very obliged 回答1: Use fgets(...) to pull in a line at a time. Note that you should check for the '\n' at the end of the line, and if there is