In C programming, the main difference between low-level I/O functions (open/close/read/write) and stream-level I/O functions (fopen/fclose/fread/fwrite) is that stream-level functions are buffered. Presumably, low-level I/O functions will incur a disk operation on each read(). Although the kernel may cache this, we cannot rely too much on it. Disk operations are expensive and so low-level I/O [...]
Posts Tagged ‘C’
A Generic Buffered Stream Wrapper
Posted in development, tagged C, myprog on October 11, 2008 | Leave a Comment »
Another Look at my old Benchmark
Posted in development, tagged benchmark, C, cpp, myprog, programming on October 7, 2008 | 24 Comments »
This is a follow-up of my previous post. Here I change the table to several charts. Hope it seems more friendly to readers. You can find the links to these libraries in that table. Their source codes, including my testing code, are available here. You may also want to see my previous posts in the [...]
Is There an Overhead to Retrieve an Element in a Struct?
Posted in development, tagged benchmark, C on October 1, 2008 | Leave a Comment »
I was wondering whether retrieving an element in a struct will incur additional overhead. And so I did the following experiment. Here the same array is sorted in two ways: with or without data retrieving from a struct. Both ways yield identical results. The question is whether the compiler knows the two ways are the [...]
Futher Discussion on Search Trees
Posted in development, tagged benchmark, C, cpp, myprog, programming on September 28, 2008 | Leave a Comment »
Over the weekend, I have done a more comprehensive benchmark of various libraries on search trees. Two AVL, seven red-black tree, one Splay tree, two treap implementations are involved, together with seven hash table libraries. As I need to present a big table, I have to write it in a free-style HTML page. You can [...]
B-tree vs. Binary Search Tree
Posted in development, tagged C, myprog, programming on September 24, 2008 | 18 Comments »
When talking about in-memory search tree, we usually think of various binary search trees: red-black tree, AVL tree, treap, splay tree and so on. We do not often think of B-tree, as B-tree is commonly introduced as an on-disk data structure rather than in-memory one. Is B-tree also a good data structure for in-memory ordered [...]
A Simple Generic Vector Container in C
Posted in development, tagged C, macro, myprog, programming on September 22, 2008 | 3 Comments »
I do not see much need to have a vector container in C as a vector is simply an array and array operations are all very simple. Nontheless, it might still better to implement one, for the sake of completeness. Here is the code. The library is almost as fast as the fastest code you [...]
Thoughts on Generic Programming in C
Posted in development, tagged C, cpp, programming, thinking on September 21, 2008 | 1 Comment »
I came across two interviews (here and here) of Alexander Stepanov, the father of STL. There are quite a lot of interesting bits. For example, he thinks C++ is the best programming language to realize his goal, but he is also strongly against OOP at the same time. In addition, he has paid a lot [...]
C Array vs. C++ Vector
Posted in development, tagged benchmark, C, cpp on September 19, 2008 | 11 Comments »
Here is a piece of source codes that compare C arrays and C++ vectors. It tests six scenarios: a) preallocated C array; b) dynamically growing C array; c) dynamical C vector calling kv_a macro (in my kvec.h); d) dynamical C vector calling kv_push macro (in my kvec.h); e) preallocated C++ vector and f) dynamically growing C++ [...]
Calculating Median
Posted in development, tagged algorithm, C, myprog, programming on September 13, 2008 | Leave a Comment »
Here is an example that google does not give me the result in the first page. I want to know how to calculate median efficiently, and so I search “c calculate median”. In the first result page, google brings me to several forums which only show very naive implementations. The 11th result, this page, is [...]
Implementing Generic Hash Library in C
Posted in development, tagged C, hash, myprog, programming on September 2, 2008 | 9 Comments »
Synopsis
Here is an simple example showing how to use khash.h library:
#include "khash.h"
KHASH_MAP_INIT_INT(32, char)
int main() {
int ret, is_missing;
khiter_t k;
khash_t(32) *h = kh_init(32);
k = kh_put(32, h, 5, &ret);
if (!ret) kh_del(32, h, k);
kh_value(h, k) = 10;
k = kh_get(32, h, 10);
is_missing = (k == kh_end(h));
k = kh_get(32, h, 5);
kh_del(32, h, k);
for (k = kh_begin(h); k != kh_end(h); ++k)
if (kh_exist(h, [...]