// returns the number of usecs of (t2 - t1)
long time_elapsed (struct timeval &t1, struct timeval &t2) {
long sec, usec;
sec = t2.tv_sec - t1.tv_sec;
usec = t2.tv_usec - t1.tv_usec;
if (usec < 0) {
--sec;
usec = usec + USEC_PER_SEC;
}
return sec*USEC_PER_SEC + usec;
}
struct timeval start_time;
struct timeval end_time;
void start_timer() {
struct timezone tz;
gettimeofday (&start_time, &tz);
}
long end_timer() {
struct timezone tz;
gettimeofday (&end_time, &tz);
return time_elapsed(start_time, end_time);
}
Archive for the ‘Computer’ Category
C++: Calculate the time difference
C++: Convert int to string using printf
You can use this function as an alternative to itoa. It’s only half as good as itoa because you can’t specify the base of your number to be converted.
sprintf takes three arguments.
The first has to be a char * variable, which means you can use a char array, but make sure it’s big enough to hold the converted number.
The second argument is a string containing a format specifier, depending on the format of the number you want to convert.
The third argument is the number you want to convert into a string.
sprintf returns the number of characters in the string (not included the null character).
This example convert a few numbers into string format, and prints out the result:
#include "stdio.h"
int main() {
char str[10];
int i;
i = sprintf(str, "%o", 15);
printf("15 in octal is %s\n", str);
printf("sprintf returns: %d\n\n", i);
i = sprintf(str, "%d", 15);
printf("15 in decimal is %s\n", str);
printf("sprintf returns: %d\n\n", i);
i = sprintf(str, "%x", 15);
printf("15 in hex is %s\n", str);
printf("sprintf returns: %d\n\n", i);
i = sprintf(str, "%f", 15.05);
printf("15.05 as a string is %s\n", str);
printf("sprintf returns: %d\n\n", i);
return 0;
}
C++:How to Use a C++ Vector to Store Data
The vector is the official array of C++. Part of the Standard Template Library, it’s a template container class that stores same-typed data in an uninterrupted region of memory. As a C++ class, it offers many useful features, such as resizing, that reduce code size and save time. Its indexing operations are as efficient as those of the dumb arrays in C.
Step 1
Include the vector header file so that your program can access the C++ class and its functions:
#include< vector >
Step 2
Create an empty vector of type int. Then create a vector with 10 copies of 7:
vector< int > v;
vector< int > v2(10, 7);
Step 3
Use v2 to find the difference between how much memory has been allocated for v2 compared to its size. The memory allocated is always greater than or equal to the size:
cout << v2.capacity() - v2.size() <
Step 4
Add two more elements to the end of v2:
v2.push_back(13);
v2.push_back(23);
Step 5
Double the size of v2 and give the additional elements a value of 64:
v2.resize(v2.size()*2, 64);
Step 6
Iterate across the vector using a special-purpose pointer called an iterator. Output the elements to the console as follows:
for( vector::iterator it = v2.begin(); it != v2.end(); it++ ){
cout << *it << " ";
}
Step 7
Erase elements 2 and 3 from the vector. Then erase all the elements at once:
v2.erase(v2.begin()+1, v2.begin()+3);
v2.clear();