Click here to Skip to main content
12,299,131 members (38,733 online)
Click here to Skip to main content

Stats

24K views
200 downloads
43 bookmarked
Posted

Session of low-level optimization of memory usage in C++ programs with total exposure

, , 22 Jun 2009 CPOL
In this article, we will try to make our algorithms work faster using the methods of low-level optimization of memory allocation in C++.
#include "cmnArena.h"
#include "string"
#include "vector"
#include "iostream"
#include "memory"
#include "windows.h"

#include "cmnArenaProvider.inc"
#include "cmnTlsBasedArena.h"


ARENA_DEFINE_GLOBAL_OPERATORS

void Function2()
{
    std::vector<char> buff(100);
    strcpy(&buff.front(), "hello");
}
void Function()
{
    std::vector<char> buff(100);
    strcpy(&buff.front(), "hello");
    Function2();

    char * p =new char[15];
    strcpy(p, "world");
    delete[] p;
}

void GlobalPerfrormanceTest()
{
    try
    {
        const int iIterCount = 1000000;
        {
            {
                Sleep(0);
                DWORD dwBegin = GetTickCount();
                for(int i =0; i<iIterCount;++i)
                {
                    Function();
                }
                DWORD dwEnd = GetTickCount();
                std::cout<<"[Global test] Normal results: "<<dwEnd-dwBegin<<"\n";
            }
            Sleep(0);
            DWORD dwBegin = GetTickCount();
            {   // arena scope
                std::auto_ptr<utils::IArena > arena(new utils::CConstArena<1024*1024*5>());
                utils::CAutoArena arena_scope(arena.get());
                for(int i =0; i<iIterCount;++i)
                {
                    utils::CAutoCleaner cleaner(arena.get());
                    Function();
                }
            }
            DWORD dwEnd = GetTickCount();
            std::cout<<"[Global test] Arena results: "<<dwEnd-dwBegin<<"\n";
        }
    }
    catch(std::exception & e)
    {
        std::cout<<e.what();
    }
}

int main()
{
    utils::CTlsArenaProvider provider;
    utils::CArenaLib arena(&provider);

    GlobalPerfrormanceTest();
 
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

No Biography provided

Apriorit Inc
Apriorit Inc.
Hungary Hungary
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.

Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.

Official site http://www.apriorit.com
Group type: Organisation

32 members


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 23 Jun 2009
Article Copyright 2009 by Victor A. Milokum, Apriorit Inc
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid