A
arnuld
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors,
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings. i wrote the same programme in C & hence
found that claim of the authors is *partial*. If we use C-style strings
in C++ instead of Library String class, then they are slow but if write
the same programme in C then C strings are "faster" than both C++
Library strings & C-style strings in C++. Below is the code of 3
programmes. i will really appreciate any comments on C++ programming,
regarding exercise, my code etc.
thanks
// C-style strings in C++
#include <iostream>
#include <cstring>
int main() {
// C style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc + 1); // for NULL terminator
// peformance test on string allocation and copy
for(size_t ix = 0; ix != 1000000; ++ix)
{
char *pc2 = new char[len + 1]; // dynamic space allocation with
NULL terminator
strcpy(pc2, pc); // copying string onto "allocated
space"
if(strcmp(pc2, pc))
;
delete [] pc2;
}
}
-------------------------------------------------------------------------
// C++ Library Strings
#include <iostream>
#include <string>
int main() {
// C++ standard library string implementation
std::string str("a very long literal string");
// performance tst on staring allocation & copy
// automatic memory management by String Library Class
for(unsigned long ix = 0; ix != 10000000; ++ix)
{
std::string str2 = str;
if(str == str2)
;
}
}
----------------------------------------------------------
// C strings
#include <string.h>
int main() {
char *ps = "a very long literal string";
int len = strlen(ps + 1);
unsigned long i;
char *ps2;
for(i = 0; i <= 10000000; ++i)
{
*ps2 = malloc(len + 1);
strcpy(ps2, ps);
if(strcmp(ps2, ps))
;
free(ps2);
ps2 = NULL;
}
}
-- arnuld
http://arnuld.blogspot.com
with "run-time shown", claim that C++ Library strings are faster than
C-style character strings. i wrote the same programme in C & hence
found that claim of the authors is *partial*. If we use C-style strings
in C++ instead of Library String class, then they are slow but if write
the same programme in C then C strings are "faster" than both C++
Library strings & C-style strings in C++. Below is the code of 3
programmes. i will really appreciate any comments on C++ programming,
regarding exercise, my code etc.
thanks
// C-style strings in C++
#include <iostream>
#include <cstring>
int main() {
// C style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc + 1); // for NULL terminator
// peformance test on string allocation and copy
for(size_t ix = 0; ix != 1000000; ++ix)
{
char *pc2 = new char[len + 1]; // dynamic space allocation with
NULL terminator
strcpy(pc2, pc); // copying string onto "allocated
space"
if(strcmp(pc2, pc))
;
delete [] pc2;
}
}
-------------------------------------------------------------------------
// C++ Library Strings
#include <iostream>
#include <string>
int main() {
// C++ standard library string implementation
std::string str("a very long literal string");
// performance tst on staring allocation & copy
// automatic memory management by String Library Class
for(unsigned long ix = 0; ix != 10000000; ++ix)
{
std::string str2 = str;
if(str == str2)
;
}
}
----------------------------------------------------------
// C strings
#include <string.h>
int main() {
char *ps = "a very long literal string";
int len = strlen(ps + 1);
unsigned long i;
char *ps2;
for(i = 0; i <= 10000000; ++i)
{
*ps2 = malloc(len + 1);
strcpy(ps2, ps);
if(strcmp(ps2, ps))
;
free(ps2);
ps2 = NULL;
}
}
-- arnuld
http://arnuld.blogspot.com