Skip to main content
Code Review

Return to Question

deleted 21 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

UPDATE:-

For me what I am trying to optimize is the other part leaving Cassandra query part as it is..

I know there might be few minimal optimization that I am supposed to make but if it is worth doing it, then I might do it now then later figuring it out.

Below is my full code. Again, I am mainly worried about my other string manipulation instead of Cassandra query part - I have removed some extra code which was not needed..

UPDATE:-

For me what I am trying to optimize is the other part leaving Cassandra query part as it is..

I know there might be few minimal optimization that I am supposed to make but if it is worth doing it, then I might do it now then later figuring it out.

Below is my full code. Again I am mainly worried about my other string manipulation instead of Cassandra query part - I have removed some extra code which was not needed..

For me what I am trying to optimize is the other part leaving Cassandra query part as it is. I know there might be few minimal optimization that I am supposed to make but if it is worth doing it, then I might do it now then later figuring it out.

Below is my full code. Again, I am mainly worried about my other string manipulation instead of Cassandra query part - I have removed some extra code which was not needed.

added 3130 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48

Below is my full code. Again I am mainly worried about my other string manipulation instead of Cassandra query part - I have removed some extra code which was not needed..

void getData(uint64_t currentTime, const std::vector<uint32_t> &shards) {
 uint16_t id;
 uint64_t lmd;
 uint32_t length;
 const char* binary_value;
 string column_key;
 string bt;
 uint64_t user_id;
 string colo;
 bool flag = false;
 try {
 
 currentTime = (currentTime / 1000) * 1000;
 
 uint64_t start = (currentTime / (60 * 60 * 1000 * 24)) % 3;
 string query;
 Cassandra::Result result;
 std::string base(boost::lexical_cast<std::string>(currentTime) + ".");
 
 for (std::vector<uint32_t>::const_iterator it = shards.begin() ; it != shards.end(); ++it) {
 string id = base + boost::lexical_cast<std::string>(*it); // use stream buffer
 query = "select name, value from keyspace.hour_"+boost::lexical_cast<std::string>(start)+" where id ='"+id+"';";
 
 result = Cassandra::execute_query(query);
 while (result && result.value()->next()) {
 char* key = 0;
 for (size_t i = 0; i < result.value()->column_count(); ++i) {
 cql::cql_byte_t* data = NULL;
 cql::cql_int_t size = 0;
 result.value()->get_data(i, &data, size);
 if (!flag) {
 key = reinterpret_cast<char*>(data);
 flag = true;
 } else {
 int index=0;
 id = Mgr::get_uint16((&data[index])); 
 index += 2;
 lmd = Mgr::get_uint64((&data[index])); 
 index += 8;
 length = Mgr::get_uint32((&data[index])); 
 index += 4;
 binary_value = reinterpret_cast<const char *>(&data[index]);
 flag = false;
 }
 }
 if(key) {
 vector<string> res;
 char* p;
 char* totken = strtok_r(key, ".", &p);
 while(totken != NULL)
 {
 res.push_back(totken);
 totken = strtok_r(NULL, ".", &p);
 }
 
 column_key= res[0];
 bt= res[1];
 user_id= atoi(res[2].c_str());
 colo = res[3];
 } 
 }
 }
 } catch (const std::exception& ex) {
 
 }
}

Below is my full code. Again I am mainly worried about my other string manipulation instead of Cassandra query part - I have removed some extra code which was not needed..

void getData(uint64_t currentTime, const std::vector<uint32_t> &shards) {
 uint16_t id;
 uint64_t lmd;
 uint32_t length;
 const char* binary_value;
 string column_key;
 string bt;
 uint64_t user_id;
 string colo;
 bool flag = false;
 try {
 
 currentTime = (currentTime / 1000) * 1000;
 
 uint64_t start = (currentTime / (60 * 60 * 1000 * 24)) % 3;
 string query;
 Cassandra::Result result;
 std::string base(boost::lexical_cast<std::string>(currentTime) + ".");
 
 for (std::vector<uint32_t>::const_iterator it = shards.begin() ; it != shards.end(); ++it) {
 string id = base + boost::lexical_cast<std::string>(*it); // use stream buffer
 query = "select name, value from keyspace.hour_"+boost::lexical_cast<std::string>(start)+" where id ='"+id+"';";
 
 result = Cassandra::execute_query(query);
 while (result && result.value()->next()) {
 char* key = 0;
 for (size_t i = 0; i < result.value()->column_count(); ++i) {
 cql::cql_byte_t* data = NULL;
 cql::cql_int_t size = 0;
 result.value()->get_data(i, &data, size);
 if (!flag) {
 key = reinterpret_cast<char*>(data);
 flag = true;
 } else {
 int index=0;
 id = Mgr::get_uint16((&data[index])); 
 index += 2;
 lmd = Mgr::get_uint64((&data[index])); 
 index += 8;
 length = Mgr::get_uint32((&data[index])); 
 index += 4;
 binary_value = reinterpret_cast<const char *>(&data[index]);
 flag = false;
 }
 }
 if(key) {
 vector<string> res;
 char* p;
 char* totken = strtok_r(key, ".", &p);
 while(totken != NULL)
 {
 res.push_back(totken);
 totken = strtok_r(NULL, ".", &p);
 }
 
 column_key= res[0];
 bt= res[1];
 user_id= atoi(res[2].c_str());
 colo = res[3];
 } 
 }
 }
 } catch (const std::exception& ex) {
 
 }
}
added 271 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48

One of my colleagues told me that you can use streambuffer for std::string, and instead of using string id in the for loop, you can declare it outside.

I am not sure whether this will make any difference or not.

void getRecord(uint64_t currentTime, const std::vector<uint32_t> &shards) {
 try {
 
 currentTime = (currentTime / 1000) * 1000;
 
 uint64_t start = (currentTime / (60 * 60 * 1000 * 24)) % 3;
 string query;
 Cassandra::Result result;
 std::string base(boost::lexical_cast<std::string>(currentTime) + ".");
 
 for (std::vector<uint32_t>::const_iterator it = shards.begin() ; it != shards.end(); ++it) {
 string id = base + boost::lexical_cast<std::string>(*it); // use stream buffer
 query = "select name, value from keyspace.hour_"+boost::lexical_cast<std::string>(start)+" where id ='"+id+"';";
 result = Cassandra::execute_query(query);
 
 }
 }
}

I am coming from a Java background, so I'm not sure which one will be more efficient compared to the other, as C++ is all about memory allocation. Is there any way to optimize the above code, even slightly?

UPDATE:-

For me what I am trying to optimize is the other part leaving Cassandra query part as it is..

I know there might be few minimal optimization that I am supposed to make but if it is worth doing it, then I might do it now then later figuring it out.

One of my colleagues told me that you can use streambuffer for std::string, and instead of using string id in the for loop, you can declare it outside.

I am not sure whether this will make any difference or not.

void getRecord(uint64_t currentTime, const std::vector<uint32_t> &shards) {
 try {
 
 currentTime = (currentTime / 1000) * 1000;
 
 uint64_t start = (currentTime / (60 * 60 * 1000 * 24)) % 3;
 string query;
 Cassandra::Result result;
 std::string base(boost::lexical_cast<std::string>(currentTime) + ".");
 
 for (std::vector<uint32_t>::const_iterator it = shards.begin() ; it != shards.end(); ++it) {
 string id = base + boost::lexical_cast<std::string>(*it); // use stream buffer
 query = "select name, value from keyspace.hour_"+boost::lexical_cast<std::string>(start)+" where id ='"+id+"';";
 result = Cassandra::execute_query(query);
 
 }
 }
}

I am coming from a Java background, so I'm not sure which one will be more efficient compared to the other, as C++ is all about memory allocation. Is there any way to optimize the above code, even slightly?

One of my colleagues told me that you can use streambuffer for std::string, and instead of using string id in the for loop, you can declare it outside.

I am not sure whether this will make any difference or not.

void getRecord(uint64_t currentTime, const std::vector<uint32_t> &shards) {
 try {
 
 currentTime = (currentTime / 1000) * 1000;
 
 uint64_t start = (currentTime / (60 * 60 * 1000 * 24)) % 3;
 string query;
 Cassandra::Result result;
 std::string base(boost::lexical_cast<std::string>(currentTime) + ".");
 
 for (std::vector<uint32_t>::const_iterator it = shards.begin() ; it != shards.end(); ++it) {
 string id = base + boost::lexical_cast<std::string>(*it); // use stream buffer
 query = "select name, value from keyspace.hour_"+boost::lexical_cast<std::string>(start)+" where id ='"+id+"';";
 result = Cassandra::execute_query(query);
 
 }
 }
}

I am coming from a Java background, so I'm not sure which one will be more efficient compared to the other, as C++ is all about memory allocation. Is there any way to optimize the above code, even slightly?

UPDATE:-

For me what I am trying to optimize is the other part leaving Cassandra query part as it is..

I know there might be few minimal optimization that I am supposed to make but if it is worth doing it, then I might do it now then later figuring it out.

deleted 78 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Loading
Tweeted twitter.com/#!/StackCodeReview/status/408088797885530113
deleted 14 characters in body
Source Link
david
  • 2k
  • 8
  • 32
  • 48
Loading
Source Link
david
  • 2k
  • 8
  • 32
  • 48
Loading
lang-cpp

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