0 Members and 1 Guest are viewing this topic.
Put the "using namespace std" inside functions, not in global scope.
Don't use strlen inside for().
Use prefix ++ instead of postfix.
QuoteDon't use strlen inside for().This technically means you must call strlen every iteration of the loop, when you only need to call it once. Some compilers (GCC) have specific optimizations for strlen inside for loops, but it is still bad practice and you should not rely on compiler to optimize it. Store value in variable and then use variable in loop!
for (i = 0, len = strlen(buff); i < len; i++)
Quote from: pubby8 on April 02, 2011, 06:28:58 pmQuoteDon't use strlen inside for().This technically means you must call strlen every iteration of the loop, when you only need to call it once. Some compilers (GCC) have specific optimizations for strlen inside for loops, but it is still bad practice and you should not rely on compiler to optimize it. Store value in variable and then use variable in loop!I agree with you, which is why it wasn't called each time. I think you read it wrong.Code: (cpp) [Select]for (i = 0, len = strlen(buff); i < len; i++) Anything called/set before the first semicolon is just ran once.I agree with the other two points you mentioned.
// Show them for (vector<server_entry>::iterator server = servers.begin(); server != servers.end(); server++) { // (refer to any of the fields mentioned by struct near beginning of file) cout << server->name << " " << server->ip << ":" << server->port << endl; }
// Request char req[100]; sprintf(req, "e%c0%c0%c0%c0%c0%c0%c0%c0%c0%c0%c0%c-1%c0%c0\n", 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169);
#define LB_SEP "\169"const char req[] = "e"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"0"LB_SEP"-1"LB_SEP"0"LB_SEP"0\n";
Iterators aren't faster, they have more overhead.
Yeah, for a vector/array/anything else which uses sequential memory - [] should always be faster than an iterator because you're directly addressing the memory offset.
#include <vector>#include <boost/progress.hpp>int main(){ const int entries = 100000000; std::vector<int> v; v.reserve(entries); for (int i = 0; i < entries; ++i) { v.push_back(i); } // benchmark counter { boost::progress_timer timer; for (int i = 0; i < entries; ++i) { v[i] += 10; // do something so loop doesn't get optimized away } } // benchmark iterator { boost::progress_timer timer; for (std::vector<int>::iterator i = v.begin(); i != v.end(); ++i) { *i += 10; // do something so loop doesn't get optimized away } }}
$ g++ -O3 benchmark.cpp && ./a.out 0.12 s0.08 s$ g++ -O0 benchmark.cpp && ./a.out0.48 s1.92 s
Interesting, I thought that since STL vectors are implemented how they are that what I said about operator[] is true (regardless of whether iterators are the same or faster or slower)My understanding of how STL vectors are implemented is that they use an array internally and when it is outgrown there is a new array that is created which is double the size (roughly speaking, the real implementation is more nuanced).
Hm, very interesting how iterators can produce faster code, however I wanted to point out that the most likely increase in speed you saw in your example was because of cache usage. If you switch the tests around, you will find that iterators are slightly slower if the test code was put before the non-iterator code.I do agree that vector iterators should be used, but I think most people overuse vectors, and iterators overall.
Hm, very interesting how iterators can produce faster code, however I wanted to point out that the most likely increase in speed you saw in your example was because of cache usage. If you switch the tests around, you will find that iterators are slightly slower if the test code was put before the non-iterator code.