Cross-platform C SDK logo

Cross-platform C SDK

Alternative to STL

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

The C++ Standard Template Library provides generic containers and algorithms as part of the language. The problem is that they cannot be used from "pure" C code, so NAppGUI provides an implementation of Arrays and Set at least as efficient as those of STL.

Result in i7-4970k Win10 x64
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
NAppGUI Containers vs STL.
- Created 2000000 elements of 328 bytes
- Starting...
- Add to ArrSt(Product) and sort: 2.160294
- Add to vector<Product> and sort: 2.499203
- Add to ArrPt(Product) and sort: 0.697777
- Add to vector<Product*> and sort: 0.541828
- Add to SetSt(Product): 2.386245
- Add to set<Product>: 2.533197
- Add to SetPt(Product): 2.861091
- Add to set<Product*>: 2.919082
Listing 1: demo/stlcmp/stlcmp.cpp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* NAppGUI containers VS STL */

#include <core/coreall.h>
#include <core/arrst.hpp>
#include <core/arrpt.hpp>
#include <core/setst.hpp>
#include <core/setpt.hpp>
#include <sewer/nowarn.hxx>
#include <vector>
#include <set>
#include <algorithm>
#include <sewer/warn.hxx>

using namespace std;

struct Product
{
    uint32_t id;
    char_t code[64];
    char_t description[256];
    real32_t price;
};

DeclSt(Product);
DeclPt(Product);

/*---------------------------------------------------------------------------*/

static void i_init(Product *product, uint32_t id, real32_t price)
{
    cassert_no_null(product);
    product->id = id;
    bstd_sprintf(product->code, 64, "Code-[%d]", id);
    bstd_sprintf(product->description, 256, "Description-[%d]", id);
    product->price = price;
}

/*---------------------------------------------------------------------------*/

static Product *i_create(uint32_t id, real32_t price)
{
    Product *product = heap_new(Product);
    i_init(product, id, price);
    return product;
}

/*---------------------------------------------------------------------------*/

static int i_compare(const Product *p1, const Product *p2)
{
    return (int)p1->id - (int)p2->id;
}

/*---------------------------------------------------------------------------*/

struct i_stl_compare
{
    inline bool operator()(const Product &lhs, const Product &rhs) const
    { return lhs.id < rhs.id; }

    inline bool operator()(const Product* lhs, const Product* rhs) const
    { return lhs->id < rhs->id; }

};

/*---------------------------------------------------------------------------*/

// All stl destructors should be called before 'core_finish',
// because this function makes a Debug memory dump.
static void i_core_finish(void)
{
    core_finish();
}

/*---------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    bool_t err;
    uint32_t n;
    uint32_t *ids;
    Product *products;
    Product **pproducts;
    ArrSt(Product) *arrst;
    ArrPt(Product) *arrpt;
    SetSt(Product) *setst;
    SetPt(Product) *setpt;
    vector<Product> stl_arrst;
    vector<Product*> stl_arrpt;
    set<Product,i_stl_compare> stl_setst;
    set<Product*,i_stl_compare> stl_setpt;
    Clock *clock;
    real64_t t;

    core_start();
    atexit(i_core_finish);

    if (argc == 2)
    {
        n = str_to_u32(argv[1], 10, &err);
        if (err == TRUE)
        {
            log_printf("Use: stlcmp [size].");
            return 0;
        }
    }
    else
    {
        n = 2000000;
    }

    bstd_printf("NAppGUI Containers vs STL.\n");

    // Create the elements. This time is out of the test
    // The elements will be shuffled randomly
    ids = heap_new_n(n, uint32_t);
    for (uint32_t i = 0; i < n; ++i)
        ids[i] = i;
    bmath_rand_seed(526);
    bmem_shuffle_n(ids, n, uint32_t);

    products = heap_new_n(n, Product);
    pproducts = heap_new_n(n, Product*);
    for (uint32_t i = 0; i < n; ++i)
    {
        i_init(&products[i], ids[i], 100.f + i);
        pproducts[i] = i_create(ids[i], 100.f + i);
    }

    arrst = arrst_create(Product);
    arrpt = arrpt_create(Product);
    setst = setst_create(i_compare, Product);
    setpt = setpt_create(i_compare, Product);

    clock = clock_create(0.);
    bstd_printf("- Created %d elements of %lu bytes\n", n, sizeof(Product));
    bstd_printf("- Starting...\n");

    // NAppGUI struct array
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
    {
        Product *p = arrst_new(arrst, Product);
        *p = products[i];
    }
    arrst_sort(arrst, i_compare, Product);
    t = clock_elapsed(clock);
    bstd_printf("- Add to ArrSt(Product) and sort: %.6f\n", t);

    // STL struct array
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        stl_arrst.push_back(products[i]);
    sort(stl_arrst.begin(), stl_arrst.end(), i_stl_compare());
    t = clock_elapsed(clock);
    bstd_printf("- Add to vector and sort: %.6f\n", t);

    // NAppGUI pointer array
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        arrpt_append(arrpt, pproducts[i], Product);
    arrpt_sort(arrpt, i_compare, Product);
    t = clock_elapsed(clock);
    bstd_printf("- Add to ArrPt(Product) and sort: %.6f\n", t);

    // STL pointer array
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        stl_arrpt.push_back(pproducts[i]);
    sort(stl_arrpt.begin(), stl_arrpt.end(), i_stl_compare());
    t = clock_elapsed(clock);
    bstd_printf("- Add to vector and sort: %.6f\n", t);

    // NAppGUI struct set
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
    {
        // TODO: review 'setst_insert'. The copy makes the insertion slower
        Product *product = setst_insert(setst, &products[i], Product);
        *product = products[i];
    }
    t = clock_elapsed(clock);
    bstd_printf("- Add to SetSt(Product): %.6f\n", t);

    // STL struct set
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        stl_setst.insert(products[i]);
    t = clock_elapsed(clock);
    bstd_printf("- Add to set: %.6f\n", t);

    // NAppGUI pointer set
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        setpt_insert(setpt, pproducts[i], Product);
    t = clock_elapsed(clock);
    bstd_printf("- Add to SetPt(Product): %.6f\n", t);

    // STL pointer set
    clock_reset(clock);
    for (uint32_t i = 0; i < n; ++i)
        stl_setpt.insert(pproducts[i]);
    t = clock_elapsed(clock);
    bstd_printf("- Add to set: %.6f\n", t);

    // Verify the sorting correctness
    clock_reset(clock);
    arrst_foreach(product, arrst, Product)
        if (product->id != product_i)
            bstd_printf("- Sorting error!!!!!\n");
    arrst_end();
    t = clock_elapsed(clock);
    bstd_printf("- Loop ArrSt(Product): %.6f\n", t);

    clock_reset(clock);
    for (size_t i = 0; i < stl_arrst.size(); ++i)
    {
        if (i != stl_arrst[i].id)
            bstd_printf("- Sorting error!!!!!\n");
    }
    t = clock_elapsed(clock);
    bstd_printf("- Loop vector: %.6f\n", t);

    clock_reset(clock);
    arrpt_foreach(product, arrpt, Product)
        if (product->id != product_i)
            bstd_printf("- Sorting error!!!!!\n");
    arrpt_end();
    t = clock_elapsed(clock);
    bstd_printf("- Loop ArrPt(Product): %.6f\n", t);

    clock_reset(clock);
    for (size_t i = 0; i < stl_arrpt.size(); ++i)
    {
        if (i != stl_arrpt[i]->id)
            bstd_printf("- Sorting error!!!!!\n");
    }
    t = clock_elapsed(clock);
    bstd_printf("- Loop vector: %.6f\n", t);

    clock_reset(clock);
    setst_foreach(product, setst, Product)
        if (product->id != product_i)
            bstd_printf("- Sorting error!!!!!\n");
    setst_fornext(product, setst, Product);
    t = clock_elapsed(clock);
    bstd_printf("- Loop SetSt: %.6f\n", t);

    uint32_t ic = 0;
    clock_reset(clock);
    for (set<Product,i_stl_compare>::iterator i = stl_setst.begin(); i != stl_setst.end(); ++i)
    {
        if (i->id != ic++)
            bstd_printf("- Sorting error!!!!!\n");
    }
    t = clock_elapsed(clock);
    bstd_printf("- Loop set: %.6f\n", t);

    clock_reset(clock);
    setpt_foreach(product, setpt, Product)
        if (product->id != product_i)
            bstd_printf("- Sorting error!!!!!\n");
    setpt_fornext(product, setpt, Product);
    t = clock_elapsed(clock);
    bstd_printf("- Loop SetPt: %.6f\n", t);

    ic = 0;
    clock_reset(clock);
    for (set<Product*,i_stl_compare>::iterator i = stl_setpt.begin(); i != stl_setpt.end(); ++i)
    {
        if ((*i)->id != ic++)
            bstd_printf("- Sorting error!!!!!\n");
    }
    t = clock_elapsed(clock);
    bstd_printf("- Loop set: %.6f\n", t);

    clock_destroy(&clock);
    arrst_destroy(&arrst, NULL, Product);
    arrpt_destroy(&arrpt, NULL, Product);
    setst_destroy(&setst, NULL, Product);
    setpt_destroy(&setpt, NULL, Product);

    for (uint32_t i = 0; i < n; ++i)
        heap_delete(&pproducts[i], Product);

    heap_delete_n(&products, n, Product);
    heap_delete_n(&pproducts, n, Product*);
    heap_delete_n(&ids, n, uint32_t);

    return 0;
}

❮ Back
Next ❯