0

I have a program where i have a struct with 2 data members

     struct myStruct{
           uint64_t promotionID;
           int32_t amount;
     };

I have to add them to an unordered_map where i had many copy operations. Right now I am searching for perfect forwarding and move, should i learn these to get rid of the copy operations of int32_t and uint64_t? Will that effect to the speed of the program (i don't have any pointer)? Here is how i add to the unordered map which makes a lot of copy operations:

void Customer::AddCustomer
   {
        my_customer_datatype addCustomer;
        addCustomer.promotionID=promotionID;
        addCustomer.amount=amount;
        customerMap->emplace(std::make_pair(customerID,addCustomer));
   }

I will read a lot of data so i need to write an efficient(in terms of speed) program. If you have any advise that i can search in detail to increase speed i will be appreciated. (I also use reserve for unordered map)

  • 2
    There is no difference between copy and move operations for such structure. And I don't think you can speed up it significantly, since you need to copy at least one pointer (probably 64-bit) anyway, and copying 96 (or even 128) bits is not much slower. So there is no point in complicating code with any tricks. – sklott Nov 25 '20 at 10:53
  • Unless you're doing literal billions of these operations it probably doesn't matter. – tadman Nov 25 '20 at 11:06
  • what about an inplace construction of my_customer_database while adding to unordered_map, will that effect the speed? – myprogramistoobusywitherrors Nov 25 '20 at 11:51
  • @myprogramistoobusywitherrors You could make your code a little bit more efficient by not creating the `std::pair` yourself: `customerMap->emplace(customerID, my_customer_datatype{promotionID, amount});` The `emplace` is already perfectly forwarding and variadic. Perfect forwarding is useful for avoiding unnecessary copying in a deduced context, i.e. in a template. Here you can't avoid copying the integers. – jignatius Nov 25 '20 at 12:34

0 Answers0