Hi there,
I am seeking a fastest way to load a BIG string and parse it as a
given format. I have a extern function which return a (char *)string in
BIG size. Now, I am going to parse it with a iterator as following
char *str = return_a_big_si ze_str();
istringstream ss(string(str), istringstream:: in);
istreambuf_iter ator<char> bit(ss), eit;
parsing(bit, eit);
I found the code shown above is so inefficient because of the big size
of str.
BTW, I also save the whole string to a file, says str.txt, and then
load the file with ifstream
std::ifstream input("str.txt" ) ;
std::istreambuf _iterator bit(input), eit;
parsing(bit, eit);
I can't believe that the later program is faster than the previous one.
Anyway, I think memory-mapped IO maybe a better choice. However, I
have no idea how memory-mapped file associated with ifstream 3 7561
it's slow because you are making a lot of copies.
is your parser templatized to use any kind of char iterator? then it
would be as easy as parsing(str, str+len). no copying required. wa***@wakun.com skrev: Hi there, I am seeking a fastest way to load a BIG string and parse it as a given format. I have a extern function which return a (char *)string in BIG size. Now, I am going to parse it with a iterator as following
IO is slow, accept it.
char *str = return_a_big_si ze_str(); istringstream ss(string(str), istringstream:: in); istreambuf_iter ator<char> bit(ss), eit; parsing(bit, eit);
I found the code shown above is so inefficient because of the big size of str.
You could always write your own iterator:
#include <iterator>
#include <stdexcept>
class cstringiterator
: public std::iterator<s td::input_itera tor_tag,char> {
private:
char const * d_cstring;
public:
cstringiterator (char const * cstring = 0)
: d_cstring(cstri ng) { }
cstringiterator (cstringiterato r const & csi)
: d_cstring(csi.d _cstring) { }
value_type operator*() throw (std::runtime_e rror) {
if(!d_cstring) throw std::runtime_er ror("Access Denied");
return *d_cstring;
}
cstringiterator & operator++() throw () {
if(d_cstring) {
if(!*++d_cstrin g) {
d_cstring = 0;
}
}
return *this;
}
cstringiterator operator++(int) throw () {
cstringiterator c(d_cstring);
++*this;
return c;
}
bool operator==(cstr ingiterator const & csi) const throw () {
return d_cstring == csi.d_cstring;
}
bool operator!=(cstr ingiterator const & csi) const throw () {
return d_cstring != csi.d_cstring;
}
};
#include <ostream>
#include <algorithm>
int main(int argc, char* argv[])
{
char const * c = "apa";
std::copy(cstri ngiterator(c),c stringiterator( ),
std::ostream_it erator<char>(st d::cout));
return 0;
}
BTW, I also save the whole string to a file, says str.txt, and then load the file with ifstream
std::ifstream input("str.txt" ) ; std::istreambuf _iterator bit(input), eit; parsing(bit, eit);
Use an iterator that utilizes internal buffers, and only reads ahead
when called for; overwriting old buffers and allocates new when needed,
unless you actually must have complete access to the entire string at
any time. I can't believe that the later program is faster than the previous one. Anyway, I think memory-mapped IO maybe a better choice. However, I have no idea how memory-mapped file associated with ifstream
Memory mapping a file is rather platform specific with its own set of
native api calls. Derive a class from std::basic_file buf that neatly
handles it all.
--
TB @ SWEDEN wa***@wakun.com wrote: char *str = return_a_big_si ze_str(); istringstream ss(string(str), istringstream:: in);
The above line create at least two copies of the string which are
all around at the same time. This is likely to cause swapping on your
system (at least if the strings are really rather large). This is an
tremendous performance hit.
istreambuf_iter ator<char> bit(ss), eit; parsing(bit, eit);
Hold it! You are parsing your string using stream *buffer* iterators,
i.e. you are not taking advantage of the formatting facilities of
streams at all? Why don't you simply pass pointers as the iterators
to the 'parsing()' function (which, of course, should be function
template). Assuming, however, that 'parsing()' is not a function
template, you still have the option to create a suitable stream buffer
which is used just for the situation described:
struct membuf:
std::streambuf
{
membuf(char* str) { this->setg(str, str, str + strlen(str)); }
};
membuf buffer(str);
std::istreambuf _iterator<char> bit(&buffer), eit;
// ...
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stephan |
last post by:
why does the following code not work????
after compiling and running it will just say killed after all my memory
filled up
any suggestions?
#include <iostream>
using namespace std;
void out_of_mem() {
|
by: Charles L |
last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.
Is this correct?
Charles L
|
by: ~Gee |
last post by:
Hi Folks!
Please see the program below:
1 #include<iostream>
2 #include<list>
3 #include <unistd.h>
4 using namespace std;
5 int main()
6 {
7 {
|
by: Vijay |
last post by:
Hi ,
I have created a program using CMapStrintToPtr where I would be
mapping
structure ptr to map string. This is just a sample program which I
thought
to avoid linear search in link list since my actual data is so huge.
I created a Windows console program. Since I am assigning a memory
block to
the structure node and I am not freeing it anyway in the program. Will
|
by: Dan Elliott |
last post by:
I am working on some tricky code and need some help from the experts.
I have several large data structures (uBLAS matrices) that must be written
to a pre-allocated (by another program) chunk of static memory. Currently
our code emulates this behavior using BOOST::serialize archives. According
to the documentation, these archive objects will write to a given ostream.
We would like to define an ostream that writes to this address without...
| |
by: Someonekicked |
last post by:
Is it possible to read a memory address with C++;
For example, If I run this code first:
*************
#include <iostream>
using namespace std;
void main()
{
int *zz = new int;
|
by: thangamani.vaiyapuri |
last post by:
Hi,
The below code snippet shows memory issues in Vector's push_back
method.
#include <iostream.h>
#include <vector>
class Base
{
|
by: marko.suonpera |
last post by:
How to create a buffer of memory in C++, whose size can dynamically
grow and shrink as needed? This is used for buffering input/output.
Several variable types, such as int and double are read and written in
this buffer in specific order.
Currently I use a fixed size array: char *data = new char;
I thought about std::vector<char> but can I directly access the memory
in vector in this fashion:
|
by: lars.uffmann |
last post by:
Hello everyone!
I just debugged a pretty huge project, eliminating basically every
memory leak that would occur with the current configuration files, at
least according to the mtrace() tool from the library <mcheck.h>.
Now the very last bugs I seem to be incapable of eliminating are:
- 0x000000000061f460 Free 387310 was never alloc'd 0x2aaaab053b53
- 0x000000000061f040 Free 387311 was never alloc'd 0x2aaaab053b53
|
by: Robert Kochem |
last post by:
Hi,
I am relative new to C++ regarding it's functions and libraries. I need to
access files larger than 4GB which is AFAIK not possible with the STL
iostream - at least not if using a 32 bit compiler. iostream was my
favorite as my code has to work on files as well as memory buffers...
Could somebody please help me what functions/classes are the best in this
case?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |