Don't copy when you can swap
2009-09-12 20:55
211 查看
Recently I worked on a cpp code beatifier, it reads in a cpp file, analysis the content and beautify the format, then write the file back. In my code there's a function like this: void beautify_a_file(const string& cpp_file)
{
string content;
// load the content
{
ifstream ifs(cpp_file.c_str());
string tmp((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
content=tmp; // this is bad, two large strings are constructed and then tmp is destroied
} process(content); // write it back
{
ofstream ofs(cpp_file.c_str());
ofs<<content;
}
} Let's look at the line content=tmp; this is a very time consuming operation, for the string here could be really big. A better way is to use string::swap, the method interchange of the two string's buffer ownership and no time-onsuming buffer copy is involved. The fix: Change: content=tmp; to: content.swap(tmp); in <string> the std::swap is overloaded to be using string::swap, so you can also use: std::swap(content, tmp); Nearly all std containers have their own swap implementations, use them, instead of copy consructor!
{
string content;
// load the content
{
ifstream ifs(cpp_file.c_str());
string tmp((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
content=tmp; // this is bad, two large strings are constructed and then tmp is destroied
} process(content); // write it back
{
ofstream ofs(cpp_file.c_str());
ofs<<content;
}
} Let's look at the line content=tmp; this is a very time consuming operation, for the string here could be really big. A better way is to use string::swap, the method interchange of the two string's buffer ownership and no time-onsuming buffer copy is involved. The fix: Change: content=tmp; to: content.swap(tmp); in <string> the std::swap is overloaded to be using string::swap, so you can also use: std::swap(content, tmp); Nearly all std containers have their own swap implementations, use them, instead of copy consructor!
相关文章推荐
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- Error “Unable to copy the file xxx from the project template to the project. Can't find the file \xxxx” when create VS template
- 每日一句:But don't you think you can finish it?
- 每日英语:When Things Go Wrong: You Can Thrive, Not Just Survive
- how to solve the problem when you have chmod the directory to 777 but the apache still can not write file
- How can you troubleshoot to try to resolve common HTTP errors that occur when working with Lotus Dom
- "Can't We Talk?" (condensed from: You Just Don't Understand)
- When you copy large files to or from earlier operating systems, the copy operation may be slower than expected on some Windows Vista-based computers
- PhantomJS Myths: Busted - don't code today what you can't debug tomorrow
- when the preview menu is diabled in BO, you can add this setting to enabled it
- What Can Happen When You Try to Convert a Standard SharePoint 2010 Farm to Enterprise
- learn English for ten years but still can not learn! Suggest you:then don't study(1)
- learn English for ten years but still can not learn! Suggest you:then don't study(2)
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- What Can Happen When You Try to Convert a Standard SharePoint 2010 Farm to Enterprise
- Can you create a second voting disk in a different ASM diskgroup when using External Redundancy in 11.2
- Where clause problem in Oracle 8i - Can you "Where case when then..."
- Don't tell your kids: Computer Games Can Make You Rich!
- CRUD, Only When You Can Afford It
- What to Do When You Have to Work with Someone You Don't Like