4种方法生成数列时间比较

十万

bash

1
2
3
4
5
6
7
8
9
10
11
# num.sh
date
for i in {1..100000}
do
echo $i
done
date

sh num.sh >num.txt
09:23:31
09:23:33 # 2秒

seq

1
2
3
4
5
6
7
8
# work.sh
date
seq 1 100000
date

sh work.sh >num.txt
09:24:20
09:24:20 # 不到1秒

python

1
2
3
4
5
6
7
8
9
10
11
12
# num.py
num = range(1,100001)
for i in num:
print(i)
# work.sh
date
python num.py
date

sh work.sh >num.txt
09:26:02
09:26:03 # 1秒

ruby

1
2
3
4
5
6
7
8
9
10
11
12
# num.rb
for i in 1..100000
p i
end
# work.sh
date
ruby num.rb
date

sh work.sh >num.txt
09:26:58
09:27:01 # 3秒

一百万

bash

1
2
3
4
5
6
7
8
9
10
11
12
# num.sh
date
for i in {1..1000000}
do
echo $i

done
date

sh num.sh >num.txt
09:14:26
09:14:51 # 25秒

seq

1
2
3
4
5
6
7
8
# work.sh
date
seq 1 1000000
date

sh work.sh >num.txt
09:07:28
09:07:29 # 1秒

python

1
2
3
4
5
6
7
8
9
10
11
12
13
# num.py
num = range(1,1000001)

for i in num:
print(i)
# work.sh
date
python num.py
date

sh work.sh >num.txt
09:09:36
09:09:38 # 2秒

ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
# num.rb
for i in 1..1000000

p i
end
# work.sh
date
ruby num.rb
date

sh work.sh >num.txt
09:12:27
09:12:46 # 19秒

两百万

bash

1
2
3
sh num.sh >num.txt
09:37:51
09:38:41 # 50秒

seq

1
2
3
sh work.sh >num.txt
09:39:48
09:39:48 # 不到1秒

python

1
2
3
sh work.sh >num.txt
09:36:24
09:36:27 # 3秒

ruby

1
2
3
4

sh work.sh >num.txt
09:34:15
09:34:59 # 44秒

五百万

bash

1
2
3
sh num.sh >num.txt
09:41:59
09:44:07 # 2分8秒

seq

1
2
3
sh work.sh >num.txt
09:41:11
09:41:12 # 1秒

python

1
2
3
sh work.sh >num.txt
09:45:39
09:45:43 # 4秒

ruby

1
2
3
4

sh work.sh >num.txt
09:46:41
09:48:18 # 1分37秒

一千万

1
2
3
4
5
6
7
8
9
10
11
12
bash
09:54:37
09:59:31 # 4分54秒
seq
09:51:55
09:51:56 # 1秒
python
09:53:32
09:53:43 # 9秒
ruby
09:55:56
09:59:41 # 3分45秒

一亿

1
2
3
4
5
6
seq
09:57:14
09:57:16 # 2秒
python
10:02:47
10:04:16 # 1分29秒