Đã vài năm kể từ khi câu hỏi này được hỏi, nhưng tôi đã biên soạn một điểm chuẩn của một số phương pháp để tính độ dài của một số nguyên.
def libc_size[i]:
return libc.snprintf[buf, 100, c_char_p[b'%i'], i] # equivalent to `return snprintf[buf, 100, "%i", i];`
def str_size[i]:
return len[str[i]] # Length of `i` as a string
def math_size[i]:
return 1 + math.floor[math.log10[i]] # 1 + floor of log10 of i
def exp_size[i]:
return int["{:.5e}".format[i].split["e"][1]] + 1 # e.g. `1e10` -> `10` + 1 -> 11
def mod_size[i]:
return len["%i" % i] # Uses string modulo instead of str[i]
def fmt_size[i]:
return len["{0}".format[i]] # Same as above but str.format
[Hàm LIBC yêu cầu một số thiết lập mà tôi chưa bao gồm]
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
1 là cảm ơn Brian Preslopsky, Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
2 là nhờ GeekTantra, và Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
3 là nhờ John La RooyĐây là kết quả:
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
[Tuyên bố miễn trừ trách nhiệm: Hàm được chạy trên các đầu vào từ 1 đến 1.000.000]
Dưới đây là kết quả cho
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
4 đến Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
5:Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
Như bạn có thể thấy,
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
6 [Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
7] là nhanh nhất, nhanh hơn một chút so với sử dụng Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
8 và nhanh hơn đáng kể so với những người khác. Ví dụ 1: Số lượng chữ số trong một số nguyên sử dụng trong khi vòng lặp
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
Đầu ra
Number of digits: 4
Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
9 được đánh giá thành 0 [sai].- Sau lần lặp đầu tiên,
0 sẽ được chia cho 10 và giá trị của nó sẽ là 345. Sau đó,Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
1 được tăng lên 1.Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
- Sau lần lặp thứ hai, giá trị của
0 sẽ là 34 vàTime for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
1 được tăng lên 2.Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
- Sau lần lặp thứ ba, giá trị của
0 sẽ là 3 vàTime for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
1 được tăng lên 3.Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
- Sau lần lặp thứ tư, giá trị của
0 sẽ là 0 vàTime for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
1 được tăng lên 4.Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
- Sau đó, biểu thức kiểm tra được đánh giá là sai và vòng lặp chấm dứt.
Ví dụ 2: Sử dụng các phương thức sẵn có
num = 123456
print[len[str[num]]]
Đầu ra
6
Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
9 được đánh giá thành 0 [sai]. Được tạo ra: Tháng 6 năm 07, 2021 | Cập nhật: Tháng 8 đến 10 tháng 8 năm 2021
- Tìm số chữ số bên trong một số có hàm
0 trong Pythonnum = 3452 count = 0 while num != 0: num //= 10 count += 1 print["Number of digits: " + str[count]]
- Tìm số chữ số bên trong một số có hàm
9 trong PythonTime for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed [fastest first]: + mod_size [1.000000x] + str_size [1.086498x] + fmt_size [1.243817x] + math_size [1.334066x] + libc_size [4.031780x] + exp_size [4.619188x]
Hướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.
Tìm số chữ số bên trong một số có hàm num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0 trong Python
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
Tìm số chữ số bên trong một số có hàm
Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
9 trong PythonHướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.
import math
n = -10
if n > 0:
digits = int[math.log10[n]]+1
elif n == 0:
digits = 1
elif n < 0:
digits = int[math.log10[-n]]+2
print[digits]
Output:
3
Hàm
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0 bên trong mô -đun num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
4 của Python được sử dụng để tìm nhật ký của cơ sở 10 cho bất kỳ số được chỉ định nào. Vì các số nguyên cũng nằm trong cơ sở 10, chúng ta có thể có số lượng các chữ số bên trong số nguyên được chỉ định với phương pháp này.Đoạn mã sau đây cho chúng ta thấy cách chúng ta có thể tìm thấy số chữ số bên trong một số có hàm
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0.Chúng tôi đã tính toán số chữ số bên trong số -10 với hàm
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0 trong mã trên. Mã này cũng xử lý trường hợp số là 0, vì nhật ký của 0 không thể được tính toán. Trước tiên chúng tôi kiểm tra xem số lớn hơn 0. Nếu số lớn hơn 0, chúng tôi tính toán các chữ số bằng cách lấy nhật ký và thêm 1 vào kết quả. Quá trình này được thực hiện vì nhật ký của bất kỳ số nào là ít hơn 1 so với số chữ số bên trong số đó.Tìm số chữ số bên trong một số có hàm Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
9 trong Python
Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
Hướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.
Hàm
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0 bên trong mô -đun num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
4 của Python được sử dụng để tìm nhật ký của cơ sở 10 cho bất kỳ số được chỉ định nào. Vì các số nguyên cũng nằm trong cơ sở 10, chúng ta có thể có số lượng các chữ số bên trong số nguyên được chỉ định với phương pháp này.n = -100.90
digits = len[str[n]]
print[digits]
Output:
Time for libc size: 1.2204 μs
Time for string size: 309.41 ns
Time for math size: 329.54 ns
Time for exp size: 1.4902 μs
Time for mod size: 249.36 ns
Time for fmt size: 336.63 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.240835x]
+ math_size [1.321577x]
+ fmt_size [1.350007x]
+ libc_size [4.894290x]
+ exp_size [5.976219x]
0Đoạn mã sau đây cho chúng ta thấy cách chúng ta có thể tìm thấy số chữ số bên trong một số có hàm
num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0.Phương pháp
Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
9 vượt trội hơn nhiều so với phương pháp num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0 để tìm số chữ số bên trong số thập phân trong Python. Lý do là phương pháp Time for libc size: 1.4686 μs
Time for string size: 395.76 ns
Time for math size: 485.94 ns
Time for exp size: 1.6826 μs
Time for mod size: 364.25 ns
Time for fmt size: 453.06 ns
In order of speed [fastest first]:
+ mod_size [1.000000x]
+ str_size [1.086498x]
+ fmt_size [1.243817x]
+ math_size [1.334066x]
+ libc_size [4.031780x]
+ exp_size [4.619188x]
9 rõ ràng, súc tích và cũng xử lý các điểm thập phân nổi không giống như phương pháp num = 3452
count = 0
while num != 0:
num //= 10
count += 1
print["Number of digits: " + str[count]]
0, phức tạp không cần thiết và không xử lý các điểm thập phân nổi.