SFML community forums
Help => Graphics => Topic started by: Keith99 on July 20, 2016, 11:53:12 pm
-
Hi, loving SFML but text processing is very slow. I expected it to be slow to a degree (I have worked with Freetype directly myself) however one thing that stands out in SFML is the setString function of sf::Text which takes a very big chunk of CPU time. On investigation I have found the slow down is actually in the copy constructor converting from std::string to sf::String. I can hold things as sf::String internally to a degree and then all is fine but at times I must use std::string and hence suffer the conversion to sf::String.
I wondered if there was a way of speeding this up? Oddly I find if I pass the return from c_str() of std::string (i.e. a char*) then it is slightly quicker.
thanks for any advice
-
String conversions are not supposed to be that slow. Can you provide a complete and minimal example that produces your performances problem? Can you also tell us what your environment is (so that we know which encodings you're dealing with)?
-
I will look to create a minimal example. The environment is Visual Studio 2015 with configuration properties set to "Use Multi-Byte Character Set" rather than Unicode.
-
By the way if I drill down using a profiler I see that sf::Text::setString() takes 21.9% of my program's time
Within that String() takes 21.2%
Within that fromAnsi() takes 20.4%
Within that operator= takes 19.8%
Within that push_back takes 19.7%
Within that Insert takes 16% made up of:
insert 6.3%
operator+ 4.2%
begin 3.5%
If I use sf::String the problem goes away so it must be to do with the conversion from std::string to sf::String. I will try to knock up a test case now
-
OK I created a basic test case just passing a vector of std::string into a function that converted it to a sf::string. For 1 million items:
With project set to use Unicode:
Copying to a std::string took 4.9 seconds
Copying to a sf::String took 40.3 seconds
With project set to use Multibyte:
Copying to a std::string took 5.1 seconds
Copying to a sf::String took 40.3 seconds
Note: done in debug mode so no optimization but of course I have not factored in things like caching etc.
void SpeedTestToSFString(const std::vector<std::string> &lotsOfStrings)
{
// Convert to sf::String
for (auto &p : lotsOfStrings)
{
sf::String sfString(p);
}
}
I will do another test but next time using the text output but it will need to wait until I have a minute
-
Benchmarking unoptimized builds is rather pointless. Re-run your test with optimizations enabled.
-
The STL is usually full of checks in debug mode, so yeah please don't benchmark debug builds...
-
Since I am comparing two things and not interested in absolute values it makes no difference. I have profiled in release mode and while the absolute numbers are naturally lower the scale of slowdown is identical.
By the way searching this forum I have found other posts from the past where people have obviously suffered the same issue with sf::Text (although sometimes without realising what it is). I am very busy but hope to explore this further and see if I can tweak the SFML code to reduce the problem and then suggest a possible fix.
-
Just for completeness my tests in release mode show sf:String copying to be 20 times slower than std::string
-
Since I am comparing two things and not interested in absolute values it makes no difference.
Since the internal code is not the same in debug and release mode, you're not comparing the same thing. STL stuff is usually slower in debug mode but this shouldn't discourage you from using it, since in release the compiler optimizes away almost everything.
Just for completeness my tests in release mode show sf:String copying to be 20 times slower than std::string
Can you show the corresponding code please?