What is the most efficient bway to get len of string ?

A lot of people wrote that this version of `get_len` is faster : int get_len(char* str) { char *cpy; cpy = str; while (*cpy) { ++cpy; } return (cpy - str); } than this one : int get_len2(char* str) { int i; i = 0; while (str[i]) { i++; } return i; } But when I check the run time, The second one seems faster... So my question is why ?? here's the main function test: int main() { clock_t begin, end; double time_spent; begin = clock(); printf("%d\n", get_len("HEEEEY")); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("%f\n", time_spent); begin = clock(); printf("%d\n", get_len2("HEEEEY")); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("%f", time_spent); return 0; } The output : 6 0.000025 6 0.000001%
I'd say it's absolutely indifferent... Also, one sample is not a sample, call those functions at least several thousands of times.

以上就是What is the most efficient bway to get len of string ?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏