the different efficiency for number digits counting with python & C

I try to write a function to count number digits,and by the way,I try to compare the efficiency of the different way. 1.the lenstr(i) way: def nDigits(i): return len(str(i)) for i in range(100000): print nDigits(i) it takes about 143.75s 2.the log10 way: import math def nDigits(i): if i > 0: n = int(math.log10(i)) + 1 elif i == 0: n = 1 else: n = int(math.log10(-i)) + 2 return n for i in range(100000): print nDigits(i) it takes about 144.35s 3.the division way: def nDigits(i): t = 0 while i > 0: t += 1 i /= 10 return t for i in range(100000): print nDigits(i) it takes about 143.43s 4.the division way in c: #include int digits(int num){ int i = 0; while (num > 0){ i += 1; num /= 10; } return i; } void main(){ int i = 0; while (i < 100000){ i += 1; printf("%d",digits(i)); } } it takes about 0.07s Is the C is 2000 times better than python...or There is a better way for python to counting number digits. thx guys,plz help me.
elif i = 0 will produce a syntax error.

以上就是the different efficiency for number digits counting with python & C的详细内容,更多请关注web前端其它相关文章!

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

评论 抢沙发

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

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

联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏