The war of words between python and ruby on the Internet has never stopped. Some people say python is slow. Some people say ruby is slower. Some people say python is beautiful. Some people say ruby is more beautiful. What’s going on??
We won’t do a large and comprehensive evaluation,Let’s take a peek from the simple brute force search program to find the method number of the N Queen problem:
The following is the code:
1 N=13
2 col=[0] *N
3 d1=[0]*(N*2)
4 d2=[0]*(N*2)
5 ret=0
6 def dfs(x):
7 global ret
8 if x==N:
9 ret& #43;=1
10 else:
11 for i in xrange(N):
12 if col[i]&# 61;=0 and d1[i+x]==0 and d2[i-x+N] ==0:
13 col[i],d1[i+x],d2[i-x+ N]=1,1,1
14 dfs(x+1)
15 col[ i],d1[i+x],d2[i-x+N]=0,0,0
16 dfs(0)
17 print ret
According to the python code, I adapted it in each language Version’ gif” alt=”” />ruby
1 N=13
2 $col=[0]*N
3 $d1=[0]*(N*2)
4 $d2=[0]*(N*2)
5 $ret=0
6 def dfs(x)
7 if x==N
8 $ret +=1
9 else
10 N.times do |i|
11 if $col[ i]==0 and $d1[i+x]==0 and $d2[i-x+N]==0
12 $col[i]=$d1[i+x]=$d2 [i-x+N]=1
13 dfs x+1
14 $col[i]=$d1[i+x]= $d2[i-x+N]=0
15 end
16 end
17 end
18 end
19 dfs 0
20 puts $ ret
C(C++)
1 #include
2 using namespace std;
3 const int N=13;
4 int col[ N],d1[N+N],d2[N+N];
5 int ret= ;0;
6 void dfs(int x ){
7 if (x==N)
8 ret++;
9 else{
10 for (int i=0;i<N;i++ )
11 if (col[i]==0 && d1 [i+x]==0 && d2[i-x+N]==0 ){
12 col[i]=d1[i+x]&��61;d2[i-x+N]=1;
13 dfs(x+ 1);
14 col[i]=d1[i+x]=d2[ i-x+N]=0;
15 }
16 }
17 }
18 int main()
19 {
20 dfs(0 );
21 printf(“%d\n“,ret);
22 return 0;
23 }
Go
1 package main
2 import “fmt“
3 const N=13
4 var col [N]int
5 var d1,d2 [N*2]int
6 var ret int
7 func dfs(x int){
8 if x==N{
9 ret++ ;
10 }else{
11 for i: =0;i<N;i++ {
12 if col[i]==0 && d1[i+x]==0 && d2[ i-x+N]==0 {
13 col[i],d1[ i+x],d2[i-x+N]=1,1,1
14 dfs(x+1)
15 col[i],d1[i&# 43;x],d2[i-x+N]=0,0,0
16 }
17 }
18 }
19 }
20 func main(){
21 dfs(0)
22 fmt.Println(ret)
23 }
The following is a comparison of code length:
268 a.py
273 a.rb
p>
333 a.go
356 a.cpp
It can be seen that “python is the shortest” and ruby is actually better in some places. It was written shorter – for example omitting the parentheses and colons – but the unfortunate use of $ and end made the code length balloon. Python’s indentation policy simplifies the input of end on the one hand and forces programmers to write elegant code on the other. But I still can’t figure out why python must have that colon.
N.times do |i|if $col[i]&# 61;=0 and $d1[i+x]==0 and $d2[i-x+N]==0$col[ i]=$d1[i+x]=$d2[i-x+ N]=1dfs x+1$col[i]= ;$d1[i+x]=$d2[i-x+N]= ;0endend
for i in xrange(N):if col[i]==0 and d1[i+x]==0 and d2[i-x+N]==0:col[i],d1[i+x],d2 [i-x+N]=1,1,1dfs(x+1)col[i],d1[i& #43;x],d2[i-x+N]=0,0,0
go and cpp are also difficult to distinguish. Go is almost the same as the scripting language in the main logic part of the function,
func dfs(x int){if x==N{ret++}else{for i:=0;i<N;i++ {if col[i]==0 && d1[i+x]==0 && d2[i-x+N]==0 {col[i],d1[i+x],d2 [i-x+N]=1,1,1dfs(x& #43;1)col[i],d1[i+x],d2[i-x+N]=0,0,0}}}
}
Except for the int& #xff0c;We can hardly call it a statically typed compiled execution language.
But the problem with go is the same as cpp – that is – in order to get a program to run you have to say a lot of “nonsense”.
package main
import “fmt“#include
using namespace std;
Then there is the operating efficiency that everyone is very concerned about
I timed it:
python 0m6.944s
ruby 0m6.728s
C(C++) 0m0. 360s
go 0m0.320s
This result is beyond the expectations of many people – go can actually run faster than c, ; And there is no imagined performance gap between python and ruby.
Of course ,Python has an acceleration method,that is to use pypy, and C++ can also turn on -O2,Let’s see the effect, #xff1a;
pypy 0m0.908s
C(C++)-O2 0m0.236s
It can be seen that, ;The enhanced versions of python and go are still far ahead of their opponents.
For the sake of compilation efficiency, go does not emphasize the use of compilation optimization. Ruby also has a jit tool – but I haven’t tested it yet.
To sum up, it’s really hard to distinguish between Python and ruby. It’s more of a matter of personal taste.
Just like materialists and idealists can never agree on whether matter determines consciousness – it is also difficult for users of python and ruby to convince each other to switch camps. More arguments are just a waste of energy.
If you don’t care about the 20 times difference in running time – then a scripting language is your best choice – because it can exchange for your coding time.
Redirect: https://www.cnblogs.com/fanhqme/archive/2012/06/17/2552466.html
}
Except for the disgraceful int,, we can hardly call it a statically typed compiled execution language.
But the problem with go is the same as cpp – that is – in order to get a program to run you have to say a lot of “nonsense”.
package main
import “fmt“#include
using namespace std;
Then there is the operating efficiency that everyone is very concerned about
I timed it:
python 0m6.944s
ruby 0m6.728s
C(C++) 0m0. 360s
go 0m0.320s
This result is beyond the expectations of many people – go can actually run faster than c, ; And there is no imagined performance gap between python and ruby.
Of course ,Python has an acceleration method,that is to use pypy, and C++ can also turn on -O2,Let’s see the effect, #xff1a;
pypy 0m0.908s
C(C++)-O2 0m0.236s
It can be seen that, ;The enhanced versions of python and go are still far ahead of their opponents.
For the sake of compilation efficiency, go does not emphasize the use of compilation optimization. Ruby also has a jit tool – but I haven’t tested it yet.
To sum up, it’s really hard to distinguish between Python and ruby. It’s more of a matter of personal taste.
Just like materialists and idealists can never agree on whether matter determines consciousness – it is also difficult for users of python and ruby to convince each other to switch camps. More arguments are just a waste of energy.
If you don’t care about the 20 times difference in running time – then a scripting language is your best choice – because it can exchange for your coding time.
Redirect: https://www.cnblogs.com/fanhqme/archive/2012/06/17/2552466.html