Giter VIP home page Giter VIP logo

dockerfiles's People

Contributors

idoop avatar

Watchers

 avatar

dockerfiles's Issues

malloc_test

#include
#include <string.h>
#include <sys/time.h>

#define MAX_BUF_SIZE (102410)
#define MID_BUF_SIZE (1024
5)
#define MIN_BUF_SIZE (1024)

#define TI(tag)
struct timeval tv_begin_##tag;
struct timeval tv_end_##tag; \
do{ \
gettimeofday(&tv_begin_##tag, NULL);
}while(0)

#define TO(tag) do {
gettimeofday(&tv_end_##tag, NULL);
std::cout << #tag << " cost time:" << \
(tv_end_##tag.tv_sec -tv_begin_##tag.tv_sec)10001000 +
(tv_end_##tag.tv_usec - tv_begin_##tag.tv_usec) <<
"us" << std::endl;
}while(0)

int main(int, char**) {

TI(malloc_max_buf);
char *p = (char*)malloc(MAX_BUF_SIZE);
TO(malloc_max_buf);
TI(malloc_mid_buf);
char *q = (char*)malloc(MID_BUF_SIZE);
TO(malloc_mid_buf);

TI(malloc_min_buf);
char *j = (char*)malloc(MIN_BUF_SIZE);
TO(malloc_min_buf);

/// malloc 向系统申请内存,形成链表 p --> q --> j
/// free p 后,系统还无法收回
TI(free_max_buf);
free(p);
TO(free_max_buf);

/// 此时再malloc一块小于p指向的内存,相当于复用p指向那块
for (int i = 0; i < 100; ++i) {
    TI(malloc_x1);
    char *x1 = (char*)malloc(MID_BUF_SIZE);
    TO(malloc_x1);

    TI(free_x1);
    free(x1);
    TO(free_x1);
}
for (int i = 0; i < 100; ++i) {
    TI(malloc_x2);
    char *x2 = (char*)malloc(MIN_BUF_SIZE);
    TO(malloc_x2);

    TI(free_x2);
    free(x2);
    TO(free_x2);
}

return 0;

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.