![]() The results are quite clear: std::copy performs as well as std::memcpy, whereas both constructors and assign are an order of magnitude slower. If (diff != 0) cout << "one of the methods produces invalid copies" << endl Ĭout << "constructor (1): " << t_constructor1 << endl Ĭout << "constructor (2): " << t_constructor2 << endl Īt my PC, compiled for 圆4 with msvc100, fully optimised, this produces the following output: constructor (1): 22388 Uint8_t diff = (c_constructor1 - c_constructor2) + Verify that all copies are correct, but also prevent the compiler Uniform_int_distribution random_byte(0x00, 0xff) įor (size_t i = 0 i destination(source) įor (size_t i = 0 i destination(source.begin(), source.end()) ĭestination.assign(source.begin(), source.end()) įor (size_t i = 0 i destination(source.size()) Ĭopy(source.begin(), source.end(), destination.begin()) Using the container's method is generally more performant than using generic algorithms, so a combination of vector::resize() and std::copy() or even memmove()/memcpy() would be a work-around, if the vendor didn't optimize the container sufficiently.īased on the suggested solutions, I decided to put together a small benchmark. Take care, that otherVect has exactly the same element type (not e.g. ![]() Which uses the default allocator for newVect.Īnother possibility is std::vector newVect nevVect.assign(otherVect.begin(), otherVect.end()) Īll of them (including the copy constuctor when otherVect uses the default allocator) should boil down to a memmove/memcpy in a good STL implementation in this case. If you just want to copy the contents of otherVect, use std::vector newVect(otherVect.begin(), otherVect.end()) The otherVect's allocator object has to be copied (and used) as well, which needs more efforts to get it performant in the STL implementation. ![]() If you use the copy constructor like in std::vector newVect(otherVect) This answer is not specific to the msvc100.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |