•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,435 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,866 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 50555 | Replies: 54
![]() |
•
•
Join Date: Dec 2007
Location: chennai , India
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 4
Referring to Mr.BountyX tips on time saving ,,
"I know this may not seem like a super time saving technique, but the way you name your variables will help save time and reduce compile time errors.."
Yes indeed. This helps a lot.
Variable Naming conventions are very important in programming and it also reduces the code reviewing time a lot .
Eg:
consider a program which has
int RecId;
For a novel programmer who is looking at your code , may wonder at a particular point of the code about the data type of the variable.
b'coz "RecId" doesnt anyhow says whether it is a integer or string or char .
Some thing like this would help the programmer easily to understand ...
int nRecID.
The ' n ' at the begin says it as integer. so when ever someone look at it he can easily understand
datatype of the variable instead of looking at the declaration again ..
Famous conventions:
eg:
int nNumber;
string sData;
float fVal
CString csData;
For Global variable's prefix it like
' g_VARIABLENAME''
"I know this may not seem like a super time saving technique, but the way you name your variables will help save time and reduce compile time errors.."
Yes indeed. This helps a lot.
Variable Naming conventions are very important in programming and it also reduces the code reviewing time a lot .
Eg:
consider a program which has
int RecId;
For a novel programmer who is looking at your code , may wonder at a particular point of the code about the data type of the variable.
b'coz "RecId" doesnt anyhow says whether it is a integer or string or char .
Some thing like this would help the programmer easily to understand ...
int nRecID.
The ' n ' at the begin says it as integer. so when ever someone look at it he can easily understand
datatype of the variable instead of looking at the declaration again ..
Famous conventions:
eg:
int nNumber;
string sData;
float fVal
CString csData;
For Global variable's prefix it like
' g_VARIABLENAME''
•
•
Join Date: Dec 2007
Location: chennai , India
Posts: 12
Reputation:
Rep Power: 1
Solved Threads: 4
C++ STL
Two Time saving Tips;
1. Try avoiding function call at the for loop condition check
Function calls are costlier . So in the above code , for every iteration,vector::size() function will be called , which is not recommended
unless or otherwise if you know that vector size is going to change inside the loop .
For example : If the vector contains 100K elements then vector::size will be called 100K times.
so the best practice will be like , getting the size of the vector before the loop begin and substitute the variable accordingly . something like this ...
2 . Use Iterators
One of the main concept of stl's are iterator. use it when ever required .
The above sample program can be written like this for the best performance and time saving .
by using iterator the element retrieval time will be faster when compare to the previous one[ vector[] ] .
The same holds good for all stl containers . (eg:stl::map)
I am so sure that the above one will improve your performance and save your time leap and bound if you are using it excessively.
•
•
•
•
vector<int> vec_nVal;
// Store elements into vector ...
// Accessing vector elements :
int ninitialize;
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; ninitialize++ )
{
int nVal = vec_nVal[ninitialize];
cout<< nVal ;
}
Two Time saving Tips;
1. Try avoiding function call at the for loop condition check
•
•
•
•
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; i++ )
Function calls are costlier . So in the above code , for every iteration,vector::size() function will be called , which is not recommended
unless or otherwise if you know that vector size is going to change inside the loop .
For example : If the vector contains 100K elements then vector::size will be called 100K times.
so the best practice will be like , getting the size of the vector before the loop begin and substitute the variable accordingly . something like this ...
•
•
•
•
"int nVecSize = vec_nVal.size();
for ( ninitialize = 0 ; ninitialize < nVecSize ; i++ ) "
2 . Use Iterators
One of the main concept of stl's are iterator. use it when ever required .
The above sample program can be written like this for the best performance and time saving .
•
•
•
•
vector<int>::iterator itrNumber;
itrNumber = vec_nVal.begin();
while( itrNumber != vec_nVal.end())
{
int nVal =*(itrNumber );
cout<<nVal;
itrNumber++
}
The same holds good for all stl containers . (eg:stl::map)
I am so sure that the above one will improve your performance and save your time leap and bound if you are using it excessively.
W.r.t std::vector.... Especially while loading ( doing a push_back )if u already know the the number of elements to be inserted then u can reserve it before u start loading. Use the reserve(...) function.
This was asked in another forum I go to and I came up with the following:
Reading contents of a file directly to a vector It's along the same lines as Naure's 'printing vector contents without a loop' trick. Use with caution though. Especially if you have a mix of types: Will store numbers in the vector of strings until it fails. Then after clearing the stream it'll read in the rest of the contents until finished. But of course this is the case in all file IO.
Reading contents of a file directly to a vector
cpp Syntax (Toggle Plain Text)
#include <fstream> #include <vector> #include <string> #include <algorithm> int main( void ) { std::ifstream in( "shorts.txt", std::ios::binary ); if ( in ) { std::vector< short > vs; vs.insert( vs.begin(), std::istream_iterator<short>(in), std::istream_iterator<short>() ); std::copy( vs.begin(), vs.end(), std::ostream_iterator<short>(std::cout, "\n")); } return 0; }
cpp Syntax (Toggle Plain Text)
#include <fstream> #include <vector> #include <string> #include <algorithm> int main( void ) { std::ifstream in( "shorts.txt", std::ios::binary ); if ( in ) { std::vector<short> vshort; std::vector<std::string> vstr; // Read shorts until fail vshort.insert( vshort.end(), std::istream_iterator<short>(in), std::istream_iterator<short>() ); // Clear stream in.clear(); // Read strings until fail, inadvertently reads to end of file. vstr.insert( vstr.end(), std::istream_iterator<std::string>(in), std::istream_iterator<std::string>() ); std::copy( vshort.begin(), vshort.end(), std::ostream_iterator<short>(std::cout, "\n") ); std::copy( vstr.begin(), vstr.end(), std::ostream_iterator<std::string>(std::cout, "\n") ); } return 0; }
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
I am the Walrus!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Timesaving Tips (C++)
- Response to time saving tips sticky (C++)
Other Threads in the C++ Forum
- Previous Thread: not sure why program crashes
- Next Thread: Alternative for cout << "";


Linear Mode