How can I improve this?

This is a discussion on How can I improve this? within the C++ Programming forums, part of the General Programming Boards category; I have the follow function that find a substr in a file. Mainly to perform lookups without loading file into ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    How can I improve this?

    I have the follow function that find a substr in a file. Mainly to perform lookups without loading file into memory.
    Code:
    typedef std::pair<long,long> FindData;
    
    //Find position of requested substr
    FindData FindSubstr(const std::string& filename, const std::string& str)
    {
        //Temporary xData structure to return
        std::pair<long,long> Temp(-1,-1);
        std::cout << "\n\nInitiating filestream for FindSubstr().\n";
        std::ifstream load(filename.c_str(),std::ios::binary);
        if ( !load.good() )
        {
            std::cout << "-->Could not open filestream. Aborting...\n\n";
            return Temp;
        }
        std::cout << "\nSearching file for substr. Please Wait...\n";
        //===========
        //Find substr
        //===========
        size_t bytes = 0;
        char byte;
        std::string Mem;
        while ( load.get(byte) )
        {
            if ( Mem.length() >= str.length() )
            {
                Mem.erase(0,1);
            }
            Mem += byte;
            ++bytes;
            //Check for match
            if ( Mem == str )
            {
                std::cout << "-->Match found after " << bytes << " bytes.\n\n";
                Temp.first = bytes-Mem.length();
                Temp.second = bytes;
                return Temp;
            }
        }
        std::cout << "-->No match found.\n\n";
        return Temp;
    }
    It works. I was just wondering what I could do to make it faster. Not really sure what to do. On files over about 3000kb you can notice the lookup. Files over 30mb it takes forever(30 seconds approx.)
    Thanks for any help or ideas.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Obviously, it's very inefficient to read and load one byte at a time.
    Last edited by Elysia; 04-19-2008 at 02:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I have to admit that the limited buffering requirement is a bit tedious but there are faster algorithms out there. You could try implementing this one or several others, depending on how this will be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  2. What do I need to improve on for the real world?
    By vhunterd in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-08-2007, 07:49 PM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. Replies: 6
    Last Post: 06-09-2006, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21