пул памяти свой делаю, пока что удалось обогнать в 5-6 раз c++:vector.push_back и в ~10 раз malloc, нужно ещё добавить 2 списка занятых и незанятых

This commit is contained in:
Kolan Sh 2011-04-29 11:43:18 +04:00
parent 6e87b9ffc9
commit 03d20062b4
4 changed files with 83 additions and 3 deletions

View File

@ -0,0 +1,64 @@
#include <stdlib.h>
#define MAX_I 0x00ffffff
#define bool char
#define TRUE 1
#define FALSE 0
struct object_s {
char val;
};
struct block_s {
bool busy;
struct object_s obj;
};
struct block_s pool[MAX_I];
void init()
{
size_t i;
for (i = 0; i < MAX_I; i++)
pool[i].busy = TRUE;
}
#define MAX_B (pool + MAX_I * sizeof(struct block_s))
inline void *alloc()
{
static struct block_s *b = pool;
struct block_s *old_b = b;
for ( ; b < MAX_B; b++)
if (!b->busy) {
b->busy = TRUE;
return &b->obj;
}
for (b = pool; b < old_b; b++)
if (!b->busy) {
b->busy = TRUE;
return &b->obj;
}
return NULL;
}
inline void free(void *p)
{
bool *b = (bool *)(p - sizeof(bool));
*b = FALSE;
}
int main()
{
unsigned long long i;
for (i = 0; i < MAX_I; i++)
alloc();
return 0;
}

View File

@ -1,7 +1,10 @@
#!/bin/bash
echo "malloc_speed_test.c test:"
cc -O2 malloc_speed_test.c -o malloc_speed_test && time ./malloc_speed_test
cc -O2 malloc_speed_test.c -o malloc_speed_test && time ./malloc_speed_test
echo -e "\n\nvector_add.cpp test:"
c++ -O2 vector_add.cpp -o vector_add && time ./vector_add
c++ -O2 vector_add.cpp -o vector_add && time ./vector_add
echo "pool1.c test:"
cc -O2 pool1.c -o pool1 && time ./pool1

View File

@ -2,7 +2,7 @@
using namespace std;
const unsigned long long MAX_I = 0x00ffffff;
const unsigned long long MAX_I = 0x0fffffff;
int main()
{

13
c/mstats_ex/mstats_ex.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main(int argc, char *argv[])
{
struct mstats ms;
ms = mstats();
return EXIT_SUCCESS;
}