From ec604df6d1b32016fdb890d8c1f9d0df7e1fd2c1 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 29 Aug 2012 23:35:22 +0200 Subject: [PATCH] Store all player IDs as ints instead of strings. Less flexibility, but chops off ~100MB RAM usage. --- bayeswf.cpp | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/bayeswf.cpp b/bayeswf.cpp index 7ae502d..bf92cc8 100644 --- a/bayeswf.cpp +++ b/bayeswf.cpp @@ -33,11 +33,11 @@ float prior_sigma; // Data waiting for insertion into the database. struct RatingDBTuple { - string player; + int player; float mu, mu_stddev; }; struct CovarianceDBTuple { - string player1, player2; + int player1, player2; float covariance; }; vector rating_db_tuples; @@ -65,7 +65,7 @@ struct match { map > matches_for_player; vector all_matches; -void dump_scores(const vector &players, const float *mu, const float *mu_stddev, int num_players) +void dump_scores(const vector &players, const float *mu, const float *mu_stddev, int num_players) { #if USE_DB for (int i = 0; i < num_players; ++i) { @@ -74,7 +74,7 @@ void dump_scores(const vector &players, const float *mu, const float *mu } #else for (int i = 0; i < num_players; ++i) { - printf("%f %f %s\n", mu[i], mu_stddev[i], players[i].c_str()); + printf("%f %f %d\n", mu[i], mu_stddev[i], players[i]); } #endif } @@ -243,7 +243,7 @@ void construct_hessian(const float *mu, int num_players) // Compute uncertainty (stddev) of mu estimates, which is sqrt((H^-1)_ii), // where H is the Hessian (see construct_hessian()). -void compute_mu_uncertainty(const float *mu, const vector &players) +void compute_mu_uncertainty(const float *mu, const vector &players) { // FIXME: Use pseudoinverse if applicable. Matrix ih = hessian.inverse(); @@ -305,8 +305,8 @@ void process_file(const char *filename) exit(1); } - vector players; - map player_map; + vector players; + map player_map; for (int i = 0; i < num_players; ++i) { char buf[256]; @@ -315,17 +315,17 @@ void process_file(const char *filename) exit(1); } - players.push_back(buf); - player_map[buf] = i; + players.push_back(atoi(buf)); + player_map[atoi(buf)] = i; } int num_matches = 0; for ( ;; ) { - char pl1[256], pl2[256]; + int pl1, pl2; int score1, score2; float weight; - if (fscanf(fp, "%s %s %d %d %f", pl1, pl2, &score1, &score2, &weight) != 5) { + if (fscanf(fp, "%d %d %d %d %f", &pl1, &pl2, &score1, &score2, &weight) != 5) { //fprintf(stderr, "Read %d matches.\n", num_matches); break; } @@ -333,11 +333,11 @@ void process_file(const char *filename) ++num_matches; if (player_map.count(pl1) == 0) { - fprintf(stderr, "Unknown player '%s'\n", pl1); + fprintf(stderr, "Unknown player '%d'\n", pl1); exit(1); } if (player_map.count(pl2) == 0) { - fprintf(stderr, "Unknown player '%s'\n", pl2); + fprintf(stderr, "Unknown player '%d'\n", pl2); exit(1); } @@ -429,12 +429,13 @@ int main(int argc, char **argv) txn.exec("TRUNCATE ratings"); pqxx::tablewriter writer(txn, "ratings"); for (unsigned i = 0; i < rating_db_tuples.size(); ++i) { - char mu_str[128], mu_stddev_str[128]; + char player_str[128], mu_str[128], mu_stddev_str[128]; + snprintf(player_str, 128, "%d", rating_db_tuples[i].player); snprintf(mu_str, 128, "%f", rating_db_tuples[i].mu); snprintf(mu_stddev_str, 128, "%f", rating_db_tuples[i].mu_stddev); vector tuple; - tuple.push_back(rating_db_tuples[i].player); + tuple.push_back(player_str); tuple.push_back(mu_str); tuple.push_back(mu_stddev_str); writer.push_back(tuple); @@ -447,12 +448,14 @@ int main(int argc, char **argv) txn.exec("CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )"); pqxx::tablewriter writer(txn, "new_covariance"); for (unsigned i = 0; i < covariance_db_tuples.size(); ++i) { - char cov_str[128]; + char player1_str[128], player2_str[128], cov_str[128]; + snprintf(player1_str, 128, "%d", covariance_db_tuples[i].player1); + snprintf(player2_str, 128, "%d", covariance_db_tuples[i].player2); snprintf(cov_str, 128, "%f", covariance_db_tuples[i].covariance); vector tuple; - tuple.push_back(covariance_db_tuples[i].player1); - tuple.push_back(covariance_db_tuples[i].player2); + tuple.push_back(player1_str); + tuple.push_back(player2_str); tuple.push_back(cov_str); writer.push_back(tuple); } -- 2.39.5