Hi all

I have profiled two source code examples [1], [2] using Valgrind's massif.

The following command has been used

# valgrind --tool=massif --time-unit=B --stacks=yes 
--massif-out-file=<name_of_executable>.massif.out ./<name_of_executable>

The output file has been printed w/ ms_print.

[3] is the print for [1] .
[4] is the print for [2] .


The difference between the two samples is that in [1] the vector is resized 
before adding elements, thus no additional memory allocations should be 
necessary for [1] .

The print expected for [1] is a rectangle, this holds true. Since the vector 
size for [2] needs to be increased while running the for-loop a stair-funtion 
should be shown, increasing steadily.

However, my graph for [2] falls in between as opposed to [1] .  Even though the same 
container (vector<>) has been used.

Is this a known behaviour for vector<>'s standard allocator to allocate and 
hand back like this?

Does anyone know why this is the case? - Sorry for might posting C++ related 
question.


Many thanks for your contributions
Eric

[1]...vectorResize.cpp

[2]...vectorNOResize.cpp

[3]...vectorResize.ms_print.txt

[4]...vectorNOResize.ms_print.txt

#include <iostream>
#include <vector>

using namespace std;

int main()
{
        clock_t t = clock();

        if (t == clock_t(-1))
        {
                cerr << "sorry, no clock" << endl;
                exit(1);
        }       

        vector<int> v2;
        for (int i = 1; i <= 100000; ++i) v2.push_back(i);

        cout << "Time taken: " << clock() - t << endl;

        return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main()
{
        clock_t t = clock();

        if (t == clock_t(-1))
        {
                cerr << "sorry, no clock" << endl;
                exit(1);
        }       

        vector<int> v1;
        v1.reserve(100000);
        for (int i = 1; i <= 100000; ++i) v1.push_back(i);

        cout << "Time taken: " << clock() - t << endl;

return 0;
}

--------------------------------------------------------------------------------
Command:            ./vectorNOResize
Massif arguments:   --time-unit=B --stacks=yes 
--massif-out-file=vectorNOResize.massif.out
ms_print arguments: vectorNOResize.massif.out
--------------------------------------------------------------------------------


    KB
768.5^                                                #                       
     |                                                #                       
     |                                                #                       
     |                                                #                       
     |                                                #                       
     |                                                #                       
     |                                                #                       
     |                                                #::::::::::::::::::::@@ 
     |                                                #: : :: : : :: : : : @  
     |                                                #: : :: : : :: : : : @  
     |                           @                    #: : :: : : :: : : : @  
     |                           @                    #: : :: : : :: : : : @  
     |                           @                    #: : :: : : :: : : : @  
     |                           @                    #: : :: : : :: : : : @  
     |                           @::::::::::::::::::::#: : :: : : :: : : : @  
     |                           @: : : : : ::::: ::: #: : :: : : :: : : : @  
     |                           @: : : : : ::::: ::: #: : :: : : :: : : : @  
     |                :::@@::::::@: : : : : ::::: ::: #: : :: : : :: : : : @  
     |                : :@ :::: :@: : : : : ::::: ::: #: : :: : : :: : : : @  
     |           :::::: :@ :::: :@: : : : : ::::: ::: #: : :: : : :: : : : @  
   0 +----------------------------------------------------------------------->MB
     0                                                                   17.12

Number of snapshots: 85
 Detailed snapshots: [14, 20, 35 (peak), 47, 50, 60, 70, 80]

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0                0                0             0            0
  1        444,136            1,888                0             0        1,888
  2        876,956            1,516                0             0        1,516
  3      1,153,592              952                0             0          952
  4      1,560,248            8,424            8,192             8          224
  5      2,007,696           16,648           16,384             8          256
  6      2,383,744           33,008           32,768             8          232
  7      2,584,768           33,008           32,768             8          232
  8      2,918,104           65,808           65,536             8          264
  9      3,338,688           65,832           65,536             8          288
 10      3,600,624           65,832           65,536             8          288
 11      3,906,204           65,836           65,536             8          292
 12      4,234,268          131,308          131,072             8          228
 13      4,714,496          131,312          131,072             8          232
 14      4,976,432          131,312          131,072             8          232
99.82% (131,072B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.82% (131,072B) 0x8049086: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.82% (131,072B) 0x80490AA: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.82% (131,072B) 0x8049228: std::vector<int, std::allocator<int> 
>::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
std::allocator<int> > >, int const&) (vector.tcc:275)
      ->99.82% (131,072B) 0x804949B: std::vector<int, std::allocator<int> 
>::push_back(int const&) (stl_vector.h:610)
        ->99.82% (131,072B) 0x8048987: main (vectorNOResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 15      5,238,368          131,312          131,072             8          232
 16      5,673,108          131,308          131,072             8          228
 17      5,946,416          131,312          131,072             8          232
 18      6,219,728          131,312          131,072             8          232
 19      6,493,040          131,312          131,072             8          232
 20      6,769,388          393,724          393,216            16          492
99.87% (393,216B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.87% (393,216B) 0x8049086: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.87% (393,216B) 0x80490AA: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.87% (393,216B) 0x8049228: std::vector<int, std::allocator<int> 
>::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
std::allocator<int> > >, int const&) (vector.tcc:275)
      ->99.87% (393,216B) 0x804949B: std::vector<int, std::allocator<int> 
>::push_back(int const&) (stl_vector.h:610)
        ->99.87% (393,216B) 0x8048987: main (vectorNOResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 21      7,062,280          262,384          262,144             8          232
 22      7,518,592          262,408          262,144             8          256
 23      8,006,852          262,444          262,144             8          292
 24      8,516,252          262,380          262,144             8          228
 25      9,040,052          262,444          262,144             8          292
 26      9,563,852          262,380          262,144             8          228
 27      9,836,992          262,408          262,144             8          256
 28     10,110,152          262,416          262,144             8          264
 29     10,383,292          262,444          262,144             8          292
 30     10,659,592          262,416          262,144             8          264
 31     11,041,472          262,408          262,144             8          256
 32     11,423,360          262,408          262,144             8          256
 33     11,614,304          262,408          262,144             8          256
 34     12,146,708          786,940          786,432            16          492
 35     12,146,708          786,940          786,432            16          492
99.94% (786,432B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.94% (786,432B) 0x8049086: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.94% (786,432B) 0x80490AA: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.94% (786,432B) 0x8049228: std::vector<int, std::allocator<int> 
>::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
std::allocator<int> > >, int const&) (vector.tcc:275)
      ->99.94% (786,432B) 0x804949B: std::vector<int, std::allocator<int> 
>::push_back(int const&) (stl_vector.h:610)
        ->99.94% (786,432B) 0x8048987: main (vectorNOResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 36     12,411,252          524,780          524,288             8          484
 37     12,817,716          524,588          524,288             8          292
 38     13,238,016          524,560          524,288             8          264
 39     13,670,476          524,588          524,288             8          292
 40     14,105,176          524,552          524,288             8          256
 41     14,548,876          524,588          524,288             8          292
 42     14,996,176          524,560          524,288             8          264
 43     15,452,476          524,588          524,288             8          292
 44     15,932,616          524,552          524,288             8          256
 45     16,420,416          524,560          524,288             8          264
 46     16,929,376          524,560          524,288             8          264
 47     17,400,692          524,700          524,288             8          404
99.92% (524,288B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.92% (524,288B) 0x8049086: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.92% (524,288B) 0x80490AA: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.92% (524,288B) 0x8049228: std::vector<int, std::allocator<int> 
>::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
std::allocator<int> > >, int const&) (vector.tcc:275)
      ->99.92% (524,288B) 0x804949B: std::vector<int, std::allocator<int> 
>::push_back(int const&) (stl_vector.h:610)
        ->99.92% (524,288B) 0x8048987: main (vectorNOResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 48     17,924,988              404                0             0          404
 49     17,951,104              280                0             0          280
 50     17,951,108              284                0             0          284
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 51     17,951,112              280                0             0          280
 52     17,951,156              236                0             0          236
 53     17,951,160              232                0             0          232
 54     17,951,164              228                0             0          228
 55     17,951,168              232                0             0          232
 56     17,951,172              236                0             0          236
 57     17,951,216              280                0             0          280
 58     17,951,220              284                0             0          284
 59     17,951,224              280                0             0          280
 60     17,951,268              236                0             0          236
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 61     17,951,272              232                0             0          232
 62     17,951,276              228                0             0          228
 63     17,951,280              232                0             0          232
 64     17,951,284              236                0             0          236
 65     17,951,328              280                0             0          280
 66     17,951,332              284                0             0          284
 67     17,951,336              280                0             0          280
 68     17,951,380              236                0             0          236
 69     17,951,384              232                0             0          232
 70     17,951,388              228                0             0          228
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 71     17,951,392              232                0             0          232
 72     17,951,396              236                0             0          236
 73     17,951,440              280                0             0          280
 74     17,951,444              284                0             0          284
 75     17,951,448              280                0             0          280
 76     17,951,492              236                0             0          236
 77     17,951,496              232                0             0          232
 78     17,951,500              228                0             0          228
 79     17,951,504              224                0             0          224
 80     17,951,508              220                0             0          220
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 81     17,951,512              216                0             0          216
 82     17,951,516              212                0             0          212
 83     17,951,520              208                0             0          208
 84     17,951,524              204                0             0          204

--------------------------------------------------------------------------------
Command:            ./vectorResize
Massif arguments:   --time-unit=B --stacks=yes 
--massif-out-file=vectorResize.massif.out
ms_print arguments: vectorResize.massif.out
--------------------------------------------------------------------------------


    KB
391.1^       #                                                                
     |       #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
     |       #::: ::: :::::: ::: :: : :::::::: :::: ::: ::: : :: : ::::::@::: 
   0 +----------------------------------------------------------------------->MB
     0                                                                   15.80

Number of snapshots: 58
 Detailed snapshots: [5 (peak), 53]

--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0                0                0             0            0
  1        444,136            1,888                0             0        1,888
  2        876,956            1,516                0             0        1,516
  3      1,153,592              952                0             0          952
  4      1,329,500              364                0             0          364
  5      1,738,948          400,436          400,000             8          428
99.89% (400,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.89% (400,000B) 0x8048E00: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.89% (400,000B) 0x8048E24: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.89% (400,000B) 0x804921E: int* std::vector<int, std::allocator<int> 
>::_M_allocate_and_copy<int*>(unsigned int, int*, int*) (stl_vector.h:767)
      ->99.89% (400,000B) 0x80492DF: std::vector<int, std::allocator<int> 
>::reserve(unsigned int) (vector.tcc:78)
        ->99.89% (400,000B) 0x804897F: main (vectorResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  6      2,051,252          400,236          400,000             8          228
  7      2,269,052          400,300          400,000             8          292
  8      2,521,052          400,300          400,000             8          292
  9      2,799,152          400,264          400,000             8          256
 10      3,102,912          400,264          400,000             8          256
 11      3,421,052          400,300          400,000             8          292
 12      3,816,152          400,272          400,000             8          264
 13      4,026,312          400,272          400,000             8          264
 14      4,265,712          400,264          400,000             8          256
 15      4,515,452          400,300          400,000             8          292
 16      4,770,152          400,272          400,000             8          264
 17      5,043,752          400,272          400,000             8          264
 18      5,341,652          400,236          400,000             8          228
 19      5,644,952          400,272          400,000             8          264
 20      5,953,652          400,236          400,000             8          228
 21      6,290,712          400,272          400,000             8          264
 22      6,661,512          400,272          400,000             8          264
 23      7,050,752          400,264          400,000             8          256
 24      7,462,052          400,236          400,000             8          228
 25      7,679,412          400,300          400,000             8          292
 26      7,918,352          400,264          400,000             8          256
 27      8,163,612          400,236          400,000             8          228
 28      8,411,112          400,272          400,000             8          264
 29      8,660,412          400,236          400,000             8          228
 30      8,911,952          400,264          400,000             8          256
 31      9,166,212          400,300          400,000             8          292
 32      9,439,352          400,272          400,000             8          264
 33      9,715,652          400,236          400,000             8          228
 34      9,993,312          400,264          400,000             8          256
 35     10,290,752          400,264          400,000             8          256
 36     10,593,612          400,236          400,000             8          228
 37     10,897,352          400,272          400,000             8          264
 38     11,205,612          400,236          400,000             8          228
 39     11,517,912          400,272          400,000             8          264
 40     11,835,612          400,236          400,000             8          228
 41     12,172,652          400,300          400,000             8          292
 42     12,543,012          400,300          400,000             8          292
 43     12,931,812          400,300          400,000             8          292
 44     13,326,452          400,236          400,000             8          228
 45     13,735,512          400,272          400,000             8          264
 46     14,168,064          400,296          400,000             8          288
 47     14,395,728          400,296          400,000             8          288
 48     14,623,392          400,296          400,000             8          288
 49     14,896,592          400,264          400,000             8          256
 50     15,072,512          400,296          400,000             8          288
 51     15,248,420          400,236          400,000             8          228
 52     15,424,344          400,272          400,000             8          264
 53     15,600,272          400,296          400,000             8          288
99.93% (400,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, 
etc.
->99.93% (400,000B) 0x8048E00: __gnu_cxx::new_allocator<int>::allocate(unsigned 
int, void const*) (new_allocator.h:88)
  ->99.93% (400,000B) 0x8048E24: std::_Vector_base<int, std::allocator<int> 
>::_M_allocate(unsigned int) (stl_vector.h:127)
    ->99.93% (400,000B) 0x804921E: int* std::vector<int, std::allocator<int> 
>::_M_allocate_and_copy<int*>(unsigned int, int*, int*) (stl_vector.h:767)
      ->99.93% (400,000B) 0x80492DF: std::vector<int, std::allocator<int> 
>::reserve(unsigned int) (vector.tcc:78)
        ->99.93% (400,000B) 0x804897F: main (vectorResize.cpp:17)
          
--------------------------------------------------------------------------------
  n        time(B)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 54     15,776,180          400,236          400,000             8          228
 55     15,952,104          400,272          400,000             8          264
 56     16,128,032          400,296          400,000             8          288
 57     16,569,444              404                0             0          404

------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its 
next-generation tools to help Windows* and Linux* C/C++ and Fortran 
developers boost performance applications - including clusters. 
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to