Skip to main content
Code Review

Return to Question

Rollback to Revision 14
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.


 // {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
 FILE* out) {
 std::ifstream file(SHOP_STOCKS_FILE);
 std::string line;
 ShopStockStruct *shopStock= NULL;
 AvailabilityStruct *availability;
 int numShopStocks = 0;
 char *itemID;
 char *itemIDprev = (char *)"";
 int shopID;
 int amount;
 std::vector<std::string> items;
 std::getline(file, line); // skip header
 while (std::getline(file, line)) {
 items = split(line, (char) '\t');
 if (items.size() < 3)
 continue;
 itemID = strdup(items[0].c_str());
 if (strcmp(itemIDprev, itemID) != 0) {
 // item changed - post process previous item
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 }
 shopID = atoi(items[1].c_str());
 amount = atoi(items[2].c_str());
 /*
 * replaced - see below
 auto shopStockFound = shopStocks.find(itemID);
 if (shopStockFound == shopStocks.end()) {
 // not found, so we need to create a new one
 shopStock = new ShopStockStruct();
 shopStock->itemID = itemID;
 // and store it, we'll add from value a bit later
 shopStocks[itemID] = *shopStock;
 }
 else {
 // found group already, get a pointer to it for adding nGroupFrom
 // value later
 shopStock = &(shopStockFound->second);
 }
 // in any case (found or not), we've to add GroupFrom value
 // to do: create struct, and store it in map
 availability = new AvailabilityStruct();
 availability->shopID = shopID;
 availability->amount = amount;
 availability->totalAmount = 0;
 availability->totalAmount10k = 0;
 (shopStock->availability)[shopID] = *availability;
 */
 auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
 shopStockFound.availability[shopID] = { 
 .shopID = shopID,
 .amount = amount,
 .totalAmount = 0,
 .totalAmount10k = 0
 };
 shopStock = &shopStockFound;
 numShopStocks++;
 }
 // process the last item if exists 
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}

BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).

Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.


 // {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
 FILE* out) {
 std::ifstream file(SHOP_STOCKS_FILE);
 std::string line;
 ShopStockStruct *shopStock= NULL;
 AvailabilityStruct *availability;
 int numShopStocks = 0;
 char *itemID;
 char *itemIDprev = (char *)"";
 int shopID;
 int amount;
 std::vector<std::string> items;
 std::getline(file, line); // skip header
 while (std::getline(file, line)) {
 items = split(line, (char) '\t');
 if (items.size() < 3)
 continue;
 itemID = strdup(items[0].c_str());
 if (strcmp(itemIDprev, itemID) != 0) {
 // item changed - post process previous item
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 }
 shopID = atoi(items[1].c_str());
 amount = atoi(items[2].c_str());
 /*
 * replaced - see below
 auto shopStockFound = shopStocks.find(itemID);
 if (shopStockFound == shopStocks.end()) {
 // not found, so we need to create a new one
 shopStock = new ShopStockStruct();
 shopStock->itemID = itemID;
 // and store it, we'll add from value a bit later
 shopStocks[itemID] = *shopStock;
 }
 else {
 // found group already, get a pointer to it for adding nGroupFrom
 // value later
 shopStock = &(shopStockFound->second);
 }
 // in any case (found or not), we've to add GroupFrom value
 // to do: create struct, and store it in map
 availability = new AvailabilityStruct();
 availability->shopID = shopID;
 availability->amount = amount;
 availability->totalAmount = 0;
 availability->totalAmount10k = 0;
 (shopStock->availability)[shopID] = *availability;
 */
 auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
 shopStockFound.availability[shopID] = { 
 .shopID = shopID,
 .amount = amount,
 .totalAmount = 0,
 .totalAmount10k = 0
 };
 shopStock = &shopStockFound;
 numShopStocks++;
 }
 // process the last item if exists 
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}

BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).

trying to rewrite my code based on Edward's solution
Source Link
pk72
  • 113
  • 7

Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.


 // {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
 FILE* out) {
 std::ifstream file(SHOP_STOCKS_FILE);
 std::string line;
 ShopStockStruct *shopStock= NULL;
 AvailabilityStruct *availability;
 int numShopStocks = 0;
 char *itemID;
 char *itemIDprev = (char *)"";
 int shopID;
 int amount;
 std::vector<std::string> items;
 std::getline(file, line); // skip header
 while (std::getline(file, line)) {
 items = split(line, (char) '\t');
 if (items.size() < 3)
 continue;
 itemID = strdup(items[0].c_str());
 if (strcmp(itemIDprev, itemID) != 0) {
 // item changed - post process previous item
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 }
 shopID = atoi(items[1].c_str());
 amount = atoi(items[2].c_str());
 /*
 * replaced - see below
 auto shopStockFound = shopStocks.find(itemID);
 if (shopStockFound == shopStocks.end()) {
 // not found, so we need to create a new one
 shopStock = new ShopStockStruct();
 shopStock->itemID = itemID;
 // and store it, we'll add from value a bit later
 shopStocks[itemID] = *shopStock;
 }
 else {
 // found group already, get a pointer to it for adding nGroupFrom
 // value later
 shopStock = &(shopStockFound->second);
 }
 // in any case (found or not), we've to add GroupFrom value
 // to do: create struct, and store it in map
 availability = new AvailabilityStruct();
 availability->shopID = shopID;
 availability->amount = amount;
 availability->totalAmount = 0;
 availability->totalAmount10k = 0;
 (shopStock->availability)[shopID] = *availability;
 */
 auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
 shopStockFound.availability[shopID] = { 
 .shopID = shopID,
 .amount = amount,
 .totalAmount = 0,
 .totalAmount10k = 0
 };
 shopStock = &shopStockFound;
 numShopStocks++;
 }
 // process the last item if exists 
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}

BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).

Based on Edward answer, I'm doing rewriting and getting rid of pointers. However, here I've got a problem coz of reference needs to be initialized at declaration. ShopStockFound is initialized later and it already contains data from next item while previous items still needs to be processed somehow.


 // {{{ ProcessShopStocks
void ProcessShopStocks(ShopStocksMap& shopStocks, const ShopsMap& shops,
 FILE* out) {
 std::ifstream file(SHOP_STOCKS_FILE);
 std::string line;
 ShopStockStruct *shopStock= NULL;
 AvailabilityStruct *availability;
 int numShopStocks = 0;
 char *itemID;
 char *itemIDprev = (char *)"";
 int shopID;
 int amount;
 std::vector<std::string> items;
 std::getline(file, line); // skip header
 while (std::getline(file, line)) {
 items = split(line, (char) '\t');
 if (items.size() < 3)
 continue;
 itemID = strdup(items[0].c_str());
 if (strcmp(itemIDprev, itemID) != 0) {
 // item changed - post process previous item
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 }
 shopID = atoi(items[1].c_str());
 amount = atoi(items[2].c_str());
 /*
 * replaced - see below
 auto shopStockFound = shopStocks.find(itemID);
 if (shopStockFound == shopStocks.end()) {
 // not found, so we need to create a new one
 shopStock = new ShopStockStruct();
 shopStock->itemID = itemID;
 // and store it, we'll add from value a bit later
 shopStocks[itemID] = *shopStock;
 }
 else {
 // found group already, get a pointer to it for adding nGroupFrom
 // value later
 shopStock = &(shopStockFound->second);
 }
 // in any case (found or not), we've to add GroupFrom value
 // to do: create struct, and store it in map
 availability = new AvailabilityStruct();
 availability->shopID = shopID;
 availability->amount = amount;
 availability->totalAmount = 0;
 availability->totalAmount10k = 0;
 (shopStock->availability)[shopID] = *availability;
 */
 auto& shopStockFound = shopStocks[itemID] = { .itemID = itemID };
 shopStockFound.availability[shopID] = { 
 .shopID = shopID,
 .amount = amount,
 .totalAmount = 0,
 .totalAmount10k = 0
 };
 shopStock = &shopStockFound;
 numShopStocks++;
 }
 // process the last item if exists 
 if (shopStock != NULL)
 PostProcessItem(*shopStock, shops, out);
 printf("Total %d ShopStocks loaded.\n", numShopStocks);
}
// }}}

BTW, after replacing some of the pointers with references the performance improved from 1.1 to 0.4 seconds. However, after modifying this function it goes back and even become worst (1.2 seconds now).

added detail from comment
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

Additionally, all shops within the same group also share their effective inventory without needing a special line in the sgm.csv file.

Additionally, all shops within the same group also share their effective inventory without needing a special line in the sgm.csv file.

Tweeted twitter.com/StackCodeReview/status/1015337824843894784
Post Reopened by 200_success, Incomputable, Edward, Phrancis, Malachi
clarified formatting convention
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
added extensive explanation of what the program is supposed to do; fixed title
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
added 88 characters in body; edited tags; edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Loading
deleted 901 characters in body; edited title
Source Link
pk72
  • 113
  • 7
Loading
performance is ok now, how about code styling?
Source Link
pk72
  • 113
  • 7
Loading
deleted 129 characters in body
Source Link
pk72
  • 113
  • 7
Loading
added 1702 characters in body; edited tags
Source Link
pk72
  • 113
  • 7
Loading
Post Closed as "Needs details or clarity" by 200_success, Billal BEGUERADJ, t3chb0t, πάντα ῥεῖ, Incomputable
deleted 8788 characters in body; edited tags
Source Link
pk72
  • 113
  • 7
Loading
edited tags
Link
Peilonrayz
  • 44.4k
  • 7
  • 80
  • 157
Loading
edited tags
Link
pk72
  • 113
  • 7
Loading
added 15430 characters in body
Source Link
pk72
  • 113
  • 7
Loading
edited tags
Link
Loading
Source Link
pk72
  • 113
  • 7
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /