Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdscatvprintf 时间复杂度问题 #33

Open
leexingliang opened this issue Jul 23, 2019 · 0 comments
Open

sdscatvprintf 时间复杂度问题 #33

leexingliang opened this issue Jul 23, 2019 · 0 comments

Comments

@leexingliang
Copy link

源码中对 sdscatvprintf 这个函数的时间复杂度分析

/* Try with buffers two times bigger every time we fail to
     * fit the string in the current buffer size. */
    while(1) {
        buf[buflen-2] = '\0';
        va_copy(cpy,ap);
        // T = O(N)
        vsnprintf(buf, buflen, fmt, cpy);
        if (buf[buflen-2] != '\0') {
            if (buf != staticbuf) zfree(buf);
            buflen *= 2;
            buf = zmalloc(buflen);
            if (buf == NULL) return NULL;
            continue;
        }
        break;
    }

buf 每次是两倍的长度增长, 时间复杂度应该是 n*logn
n 代表 fmt 字符串的长度

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant