c - 优化parser函数c程序

标签 c optimization

gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
c89

你好,

我想知道我是否可以再优化这段代码。由于这是在快速事务服务器中,因此每秒会有很多调用。因此解析器必须非常快速和优化。

我想知道我是否可以做出任何改进。

包含测试用例的完整代码。 g_get_dnis_user_part 函数是我要优化的。

我希望这是发帖的正确论坛。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif

static int g_get_dnis_user_part(const char *dnis, char *user_part, size_t size);

int main(void)
{
    /* Test cases */
    const char *dnis_test1 = "0846372573@10.1.8.34";
    const char *dnis_test2 = "084637257310.1.8.34";
    const char *dnis_test3 = "084e672573@10.1.8.34";
    const char *dnis_test4 = "";
    const char *dnis_test5 = "084637257310.1.8.34@";
    size_t passes = 0;
    size_t failures = 0;

#define MAX_ADDRESS_LEN 32

    char user_part[MAX_ADDRESS_LEN];

    memset(user_part, 0, sizeof user_part);
    if(g_get_dnis_user_part(dnis_test1, user_part, MAX_ADDRESS_LEN) == TRUE) {
        printf("TEST 1 PASSED [ %s ] [ %s ]\n", dnis_test1, user_part);
        passes++;
    }
    else {
        printf("TEST 1 FAILED [ %s ] [ %s ]\n", dnis_test1, user_part);
        failures++;
    }

    memset(user_part, 0, sizeof user_part);
    if(g_get_dnis_user_part(dnis_test2, user_part, MAX_ADDRESS_LEN) == TRUE) {
        printf("TEST 2 PASSED [ %s ] [ %s ]\n", dnis_test2, user_part);
        passes++;
    }
    else {
        printf("TEST 2 FAILED [ %s ] [ %s ]\n", dnis_test2, user_part);
        failures++;
    }

    memset(user_part, 0, sizeof user_part);
    if(g_get_dnis_user_part(dnis_test3, user_part, MAX_ADDRESS_LEN) == TRUE) {
        printf("TEST 3 PASSED [ %s ] [ %s ]\n", dnis_test3, user_part);
        passes++;
    }
    else {
        printf("TEST 3 FAILED [ %s ] [ %s ]\n", dnis_test3, user_part);
        failures++;
    }

    memset(user_part, 0, sizeof user_part);
    if(g_get_dnis_user_part(dnis_test4, user_part, MAX_ADDRESS_LEN) == TRUE) {
        printf("TEST 4 PASSED [ %s ] [ %s ]\n", dnis_test4, user_part);
        passes++;
    }
    else {
        printf("TEST 4 FAILED [ %s ] [ %s ]\n", dnis_test4, user_part);
        failures++;
    }

    memset(user_part, 0, sizeof user_part);
    if(g_get_dnis_user_part(dnis_test5, user_part, MAX_ADDRESS_LEN) == TRUE) {
        printf("TEST 5 PASSED [ %s ] [ %s ]\n", dnis_test5, user_part);
        passes++;
    }
    else {
        printf("TEST 5 FAILED [ %s ] [ %s ]\n", dnis_test5, user_part);
        failures++;
    }

    printf("ALL TEST COMPLETED PASSES [ %ld ] FAILURES [ %ld ]\n", passes, failures);

    return 0;
}

/* Get the user part from the complete dnis number
   0846372573@10.1.8.34 -> 0846372573 nul terminated */
static int g_get_dnis_user_part(const char *dnis, char *user_part, size_t size)
{
    size_t i = 0;
    int status = FALSE;

    /* Make room for the nul terminator */
    if(size > 1) {
        size--;
    }
    else {
        return status;
    }

    for(i = 0; i < size; i++) {
        /* Check for valid digit */
        if(isdigit(*dnis) != 0) {
            user_part[i] = *dnis;
        }
        else {
            if(*dnis == '@') {
                /* We are at the end */
                status = TRUE;
                break;
            }
            else {
                /* Not a digit or @ - corrupted dnis string */
                status = FALSE;
                break;
            }
        }

        /* Next character */
        dnis++;
    }

    /* nul terminate the string */
    user_part[i++] = '\0';

    /* Status FALSE indicates that the @ was not found or possible corruption with dnis string */
    return status;
}

非常感谢您的任何建议,

最佳答案

不要过度优化。这是一个非常简单的函数,它对小到足以放入缓存的数据集进行操作。很有可能它的运行速度和它已经可以的一样快(假设优化了编译器标志等)。但更重要的是,这只是您整个计划的一小部分。不要花费所有精力在汇编程序中重写它并仔细阅读 x86 体系结构手册以确保 CPU 流水线保持完全完整或者当您确定在其他地方有更多唾手可得的成果时。首先分析,然后优化分析器认为你太慢的地方。

关于c - 优化parser函数c程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9937922/

相关文章:

c++ - 从/向文件读写 double

sql - Postgres 查询优化

c++ - 乘以 2 个相同/不同矩阵的性能

python-3.x - pyomo 列出可用的求解器

对字符数组元素类型的混淆

C编程,如何在等待用户输入时运行for循环

function - 编译器如何将函数消除应用于不纯函数?

python-2.7 - 当模型给出不可行解时获取约束解

c - 不带参数的结构对齐和 16 位目标上的对齐

c - WinAPI 获取鼠标光标图标