public
Created

Librope vs SGI ropes vs C strings

  • Download Gist
Results
1 2 3 4 5 6 7 8 9 10 11 12 13
$ ./a.out -b
Benchmarking...
benchmarking librope
did 500000 iterations in 157.574000 ms: 3.173112 Miter/sec
final string length: 5799
 
benchmarking c string
did 500000 iterations in 1556.024000 ms: 0.321332 Miter/sec
final string length: 5799
 
benchmarking sgirope
did 500000 iterations in 8442.429000 ms: 0.059225 Miter/sec
final string length: 5799
gistfile1.cpp
C++
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
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#include "rope.h"
#include "tests.h"
 
#include <ext/rope>
 
// Wrapper for rope
static void *_rope_create() {
return rope_new();
}
 
static void _rope_insert(void *r, size_t pos, const uint8_t *str) {
rope_insert((rope *)r, pos, str);
}
static void _rope_del(void *r, size_t pos, size_t len) {
rope_del((rope *)r, pos, len);
}
static void _rope_destroy(void *r) {
rope_free((rope *)r);
}
 
static size_t _rope_num_chars(void *r) {
return rope_char_count((rope *)r);
}
 
// Wrapper for a vector-based string
 
// Private rope methods, stolen for utf8 support in the string.
static size_t codepoint_size(uint8_t byte) {
if (byte <= 0x7f) { return 1; }
else if (byte <= 0xdf) { return 2; }
else if (byte <= 0xef) { return 3; }
else if (byte <= 0xf7) { return 4; }
else if (byte <= 0xfb) { return 5; }
else if (byte <= 0xfd) { return 6; }
else {
// The codepoint is invalid... what do?
//assert(0);
return 1;
}
}
 
// This little function counts how many bytes the some characters take up.
static size_t count_bytes_in_chars(const uint8_t *str, size_t num_chars) {
const uint8_t *p = str;
for (int i = 0; i < num_chars; i++) {
p += codepoint_size(*p);
}
return p - str;
}
 
static size_t utf8_strlen(const uint8_t *str) {
const uint8_t *p = str;
while (*p) {
p += codepoint_size(*p);
}
return p - str;
}
 
typedef struct {
uint8_t *mem;
size_t capacity;
size_t len;
size_t num_chars;
} _string;
 
static void *_str_create() {
_string *s = (_string *)malloc(sizeof(_string));
s->capacity = 64; // A reasonable capacity considering...
s->mem = (uint8_t *)malloc(s->capacity);
s->len = 0;
s->num_chars = 0;
return s;
}
 
static void _str_insert(void *r, size_t pos, const uint8_t *str) {
_string *s = (_string *)r;
size_t num_inserted_bytes = strlen((char *)str);
// Offset to insert at in the string.
size_t offset = count_bytes_in_chars(s->mem, pos);
size_t end_size = s->len - offset;
// Resize if needed.
s->len += num_inserted_bytes;
if (s->len > s->capacity) {
while (s->len > s->capacity) {
s->capacity *= 2;
}
s->mem = (uint8_t *)realloc(s->mem, s->capacity);
}
s->num_chars += utf8_strlen(str);
memmove(&s->mem[offset + num_inserted_bytes], &s->mem[offset], end_size);
memcpy(&s->mem[offset], str, num_inserted_bytes);
}
 
static void _str_del(void *r, size_t pos, size_t len) {
_string *s = (_string *)r;
// Offset to delete at in the string.
size_t offset = count_bytes_in_chars(s->mem, pos);
size_t num_bytes = count_bytes_in_chars(s->mem + offset, len);
size_t end_size = s->len - offset - num_bytes;
 
memmove(&s->mem[offset], &s->mem[offset + num_bytes], end_size);
s->len -= num_bytes;
s->num_chars -= len;
}
 
static void _str_destroy(void *r) {
_string *s = (_string *)r;
free(s->mem);
free(s);
}
 
static size_t _str_num_chars(void *r) {
_string *s = (_string *)r;
return s->num_chars;
}
 
// SGI C++ rope
static void *_sgi_create() {
return new __gnu_cxx::crope();
}
 
static void _sgi_insert(void *r, size_t pos, const uint8_t *str) {
__gnu_cxx::crope *rope = (__gnu_cxx::crope *)r;
rope->insert(pos, (const char *)str);
}
static void _sgi_del(void *r, size_t pos, size_t len) {
__gnu_cxx::crope *rope = (__gnu_cxx::crope *)r;
rope->erase(pos, len);
}
static void _sgi_destroy(void *r) {
__gnu_cxx::crope *rope = (__gnu_cxx::crope *)r;
delete rope;
}
 
static size_t _sgi_num_chars(void *r) {
__gnu_cxx::crope *rope = (__gnu_cxx::crope *)r;
return rope->size();
}
 
 
struct rope_implementation {
const char *name;
void* (*create)();
void (*insert)(void *r, size_t pos, const uint8_t *str);
void (*del)(void *r, size_t pos, size_t len);
void (*destroy)(void *r);
size_t (*num_chars)(void *r);
} types[] = {
{ "librope", &_rope_create, &_rope_insert, &_rope_del, &_rope_destroy, &_rope_num_chars },
{ "c string", &_str_create, &_str_insert, &_str_del, &_str_destroy, &_str_num_chars },
{ "sgirope", &_sgi_create, &_sgi_insert, &_sgi_del, &_sgi_destroy, &_sgi_num_chars },
};
 
void benchmark() {
printf("Benchmarking...\n");
//long iterations = 20000000;
long iterations = 500000;
struct timeval start, end;
uint8_t *strings[100];
for (int i = 0; i < 100; i++) {
size_t len = 1 + random() % 2;//i * i + 1;
strings[i] = (uint8_t *)calloc(1, len + 1);
random_ascii_string(strings[i], len + 1);
}
// We should pick the same random sequence each benchmark run.
unsigned long *rvals = (unsigned long *)malloc(sizeof(long) * iterations);
for (int i = 0; i < iterations; i++) {
rvals[i] = random();
}
 
for (int t = 0; t < sizeof(types) / sizeof(types[0]); t++) {
//for (int t = 0; t < 1; t++) {
printf("benchmarking %s\n", types[t].name);
void *r = types[t].create();
gettimeofday(&start, NULL);
for (long i = 0; i < iterations; i++) {
if (types[t].num_chars(r) == 0 || i % 20 > 0) {
// insert. (Inserts are way more common in practice than deletes.)
uint8_t *str = strings[i % 100];
types[t].insert(r, rvals[i] % (types[t].num_chars(r) + 1), str);
} else {
size_t pos = rvals[i] % types[t].num_chars(r);
size_t length = MIN(types[t].num_chars(r) - pos, 1 + (~rvals[i]) % 53);
types[t].del(r, pos, length);
}
//printf("%s\n", rope_createcstr(r, NULL));
}
gettimeofday(&end, NULL);
 
double elapsedTime = end.tv_sec - start.tv_sec;
elapsedTime += (end.tv_usec - start.tv_usec) / 1e6;
printf("did %ld iterations in %f ms: %f Miter/sec\n",
iterations, elapsedTime * 1000, iterations / elapsedTime / 1000000);
printf("final string length: %zi\n", types[t].num_chars(r));
 
types[t].destroy(r);
}
for (int i = 0; i < 100; i++) {
free(strings[i]);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.