Efficiency of STL on VxWorks

xiaoxiao2021-03-05  24

Indulge in STL powerful features and generic programming ideas, use STL when writing programs on VxWorks, everything is normal, except for final performance testing, I don't believe how the program is so slow.

To this end, the following small procedures are specifically prepared for comparison testing. First look at the test results:

Testmap Last: 201

TestVector Last: 116

Test Last: 2

In addition, pay attention to the size of the generated .o file:

Only TEST: 38K

Contains Map: 98K

Include Vector: 68K

(As mentioned on VxWorks FAQ, add -fno-exceptions -fno-rtti when compiling, there seems to be no effect)

The test procedure is as follows:

#include

#include

#include

#include "ticklib.h"

Using namespace std;

#define test_map

#define test_vector

#ifdef test_map

INT TestMap ()

{

Map test_map;

Test_map.insert (Make_Pair (1, 111));

Test_map.insert (Make_Pair (2, 222));

Test_map.insert (Make_Pair (3, 333));

Map :: itrator it = Test_map.find (3);

INT RET = (* it). Second;

// Printf ("% d / n", return;

Return 0;

}

#ENDIF

#ifdef test_vector

Int testvector ()

{

Vector test_vec;

Test_vec.push_back (111);

TEST_VEC.PUSH_BACK (222);

Test_vec.push_back (333);

Vector :: item it = test_vec.begin ();

For (; it! = test_vec.end (); IT) if (* it == 333);

Return 0;

}

#ENDIF

Int test ()

{

Int Arr [5];

Arr [0] = 111;

Arr [1] = 222;

Arr [2] = 333;

For (int i = 0; i <3; i ) IF (Arr [i] == 333);

// Printf ("% d / n", arr [2]);

Return 0;

}

int main ()

{

INT I, NUM = 100000;

#ifdef test_map

INT map_tick = tickget ();

For (i = 0; i

Testmap ();

Printf ("Testmap Last:% D / N", Tickget () - MAP_TICK);

#ENDIF

#ifdef test_vector

INT vec_tick = tickget ();

For (i = 0; i

TestVector ();

Printf ("TestVector Last:% D / N", Tickget () - VEC_TICK); # ENDIF

INT base_tick = tickget ();

For (i = 0; i

Test ();

Printf ("Test Last:% U / N", Tickget () - BASE_TICK);

}

This result is frustrated. However, how can it be so slow? And why is compiled?

Think about it, the generic programming is used in the generic programming, although the template will not "simple" like macro, however, each instantiates a container, which is equivalent to use this instance, replacing the implementation of the container all macro. . This is also why the class defined using the template can only be in the header file: it is just a generalized type (including implementation code). (I didn't know how to understand the specific implementation of the template, just speculate)

This may explain the compiled file, but why is it so slow? At least, can Vector should be so slow? With regard to STL, there is saying that it is said that its efficiency is the same as you implement. Take a closer look, in the push_back, STL needs to use allocator to allocate memory; begin, End is a function call. These operations should be such efficiency should not be unreasonable.

STL is a good thing, but it is not what it wants in any case.

转载请注明原文地址:https://www.9cbs.com/read-38703.html

New Post(0)