My options for improving code speed.

For code related discussions and questions
User avatar
moltengear
Trained
Trained
Posts: 147
Joined: 22 Jul 2017, 15:05

Re: My options for improving code speed.

Post by moltengear » 06 Apr 2019, 23:04

Cyp wrote:
06 Apr 2019, 20:02
Might be fairer to compare to sqrtf than to sqrt. I'd naïevely guess sqrtf to be faster and more accurate than Q_rsqrt.

I'm not sure square root calculations are the main bottleneck in the game.
Remade! There was a difference of 10 times. Now more than 13 times. So it works!

Cyp
Evitcani
Evitcani
Posts: 759
Joined: 17 Jan 2010, 23:35

Re: My options for improving code speed.

Post by Cyp » 07 Apr 2019, 08:59

faster ≠ works

Changing slightly, so it could actually compile:

Code: Select all

// g++ -O3 -march=native -fno-strict-aliasing -DQ -s -o sqrtf sqrtf.cpp -Wall -Wextra -fno-math-errno

#include <cmath>
#include <iostream>


float Q_rsqrt(float number)
{
	int32_t i;
	float x2, y;
	const float threehalfs = 1.5F;

	x2 = number * 0.5F;
	y = number;
	i = *(int32_t *)&y;
	i = 0x5f3759df - (i >> 1);
	y = *(float *)&i;
	y = y * (threehalfs - (x2 * y * y));

	return y;
}

int main() {
	float a = 0, t = 0;
#ifdef Q
	for (int x = 0; x < 1000000000; x++)
	{
		a = Q_rsqrt(t);
		t = t + 0.01;
		float z = Q_rsqrt(a + t);
		a = z;
	}
#else
	for (int x = 0; x < 1000000000; x++)
	{
		a = 1.f/sqrtf(t);
		t = t + 0.01f;
		float z = 1.f/sqrtf(a + t);
		a = z;
	}
#endif
	std::cout << a << std::endl;
}
I do actually measure the Q_rsqrt version to be about 13% faster, but the final result doesn't exactly agree (gives 0.00194982 instead of 0.00195312). Even if it was infinitely faster, I don't think it would affect the game performance significantly.

——————————————

Note that without -fno-math-errno, the sqrtf version is about 133% slower. The game only uses errno in one place, so should probably be compiled with -fno-math-errno if that can be removed.

User avatar
moltengear
Trained
Trained
Posts: 147
Joined: 22 Jul 2017, 15:05

Re: My options for improving code speed.

Post by moltengear » 31 Aug 2019, 15:09

As far as I know, sound support is implemented in the same thread as the entire game code.
I wonder how much CPU time percentage spent on providing sound in the game?

Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3775
Joined: 03 Aug 2006, 19:39

Re: My options for improving code speed.

Post by Per » 31 Aug 2019, 15:43

moltengear wrote:
31 Aug 2019, 15:09
As far as I know, sound support is implemented in the same thread as the entire game code.
I wonder how much CPU time percentage spent on providing sound in the game?
Very little. Graphics, pathfinding and AI are the big consumers.

User avatar
moltengear
Trained
Trained
Posts: 147
Joined: 22 Jul 2017, 15:05

Re: My options for improving code speed.

Post by moltengear » 01 Sep 2019, 16:03

Yes, shadows are calculated on the processor.

Post Reply