Dart language opens the way of research.

The dart language has never been sullen and has grown silently, but with the slow development of Google, the future of this language is getting brighter and brighter. It is a full-stack language that can be used for both front-end and back-end.

I was bored today and started to study this language. What kind of language is dart? It can be compiled into JS, so it can be the choice for WEB development, and it can also have a virtual machine locally, and it can also develop programs such as servers.

In the current plethora of languages, when any language comes out, it must be compared with other languages ​​to see what advantages it has and what kind of uses it has.

Let's look at the efficiency first.

At present, the first execution efficiency is C/C++ (for those of us who are engaged in the Internet, I believe that very few people use assembly), followed by RUST, GO, OBJECT-C, etc. Followed by JAVA, C#, or delphi and so on.

It is best to compare JAVA, because JAVA language is widely used in the current Internet: website server, game server, can also do scientific data analysis, and can make high-performance data queue cache, so many successful examples above , which shows that its running speed is extremely extraordinary.

As for why Android is so stuck, it is because the comparison with Apple has kicked Android down, creating a bad impression. Thinking about it, Apple mobile phones cost more than 6,000 yuan at that time, while Android phones were only about 1,000 yuan at that time, and even many people Buying a few hundred yuan of Android phones, the price difference is so big, Android can't even think about it, Android is a card, and JAVA has a black pot on its back.

1: Integer calculation comparison

JAVA code:

		long sum=0;
		long t = (System.currentTimeMillis());
		for (int i = 0; i <20000; i++) {
			for (int n = 0; n < 20000;n++) {
				sum=(sum+i*n);
			 }
			}
	
		System.out.println(sum);
		System.out.println((float) (System.currentTimeMillis() - t) );
	}

Results of the:

39996000100000000
1466.0 (Note: milliseconds)

The Dart language code is as follows:

import 'dart:math';
void main(){
  DateTime now = new DateTime.now();
  var sum=0;

for (var i = 0; i < 20000; i++) {
    for (var n = 0; n < 20000; n++) {
      sum=sum+i*n;
  }
}


DateTime now1 = new DateTime.now();

print(sum);
print(now1.difference(now).inMicroseconds/1000);
print(now);
print(now1);
}

Results of the

39996000100000000

982.608 (note: milliseconds)

2018-03-07 02:04:18.102754

2018-03-07 02:04:19.085362

In contrast, in integer calculations, dart is faster than JAVA, and floating-point calculations in JAVA are much faster.

Change the code in JAVA to: sum=Math.sqrt(sum+i*n); Change the code in Dart to: sum= sqrt( sum+i*n);

The speed of JAVA execution is 6755.0 milliseconds (6.8 seconds), while the speed of Dart is 9342.055 milliseconds (9.3 seconds), you can see that the speed of JAVA is one third faster.

This is enough to show that Dart is competent for server language development, because even a language as slow as python is more and more developed as a game server and website server, and it takes 5 minutes for PYTHON to execute the above floating-point code.

1: dart data variable.

To define variables in dart, you can use var or specify the type. For the convenience of development, use var directly. If the server is being developed, try to specify the type.

(In order to achieve high performance of the program, specify the type as much as possible, which can reduce the burden and running time of the compiler. For example, in the above code, var sum=0, and double sum=0.0, their execution time is different, specifying The type's runtime was 9.3 seconds, and with the var keyword, the time reached 11.3 seconds.)

In dart, everything is an object, such as functions, variables, etc. All objects inherit the Object class.

Final can be used in dart, its value cannot be changed and should be specified during initialization.

For example the following code:

final j=10;
j=10;
print(j);

The compilation directly reports an error, breaking at exception: NoSuchMethodError. Even if the value of J has not changed, it will report an error, because the assignment operation makes the object change.

const can be used in dart, and its value cannot be changed.

The difference between them is that the variable that defines final is defined by initialization, and the const type is a compile-time constant, so its value cannot be changed. Const and final can do operations, such as the following code:

const aa=10;
const bb=20;
const cc=aa*bb;
print(cc);

final a=10;
final b=20;
final c=a*b;
print(c);


The result is: 200, 200

2: Dart's built-in types.

1: Integer.

Integer range: -9223372036854775808---9223372036854775807, inclusive of these two numbers. On a 64-bit system, it is between -2**63 and 2**63-1.

For example, when defining, you can also use hexadecimal:

int x = 110000;
int hex = 0xFFFF;

print(hex);

2: double type

 Definitions to note:

double a =10;//错误的定义
double B=3+5;//错误的定义
double c=3*5;//错误的定义

In this case, the definition is wrong, and it will prompt: A value of type 'int' can't be assigned to a variable of type 'double

It must be correctly defined like this:

double a =10.0;
double b =1+2.5;
double c=3/6;

When adding double and int, pay attention to the following issues:

double a =1.2;
int d=10;
a=a+d;
print(a);
输出11.2


double a =1.2;
int d=10;
d=a+d;
print(a);
编译器报错

double can be equal to double+int. In some other languages, the type must be strictly controlled. For example, in the GO language, this way of writing is wrong, and must be double=double+double or float64=float64+float64.

3: string type;

The strings in dart use the utf-16 encoding format, which is a bit different.

Dart string definition, you can use single quotes or double quotes.

var s1 = 'hello ';
var s2 = "world.";


print(s1);
print(s2);
print(s1+s2);

输出结果:
hello 
world.
hello world.

The conversion of char characters or unicode encoded characters is a bit different from other languages, see the following code:

var s1 = 'hello ';
var s2 = "world.";
String a='A';

print(s1);
print(s2);
print(s1+s2);
print(a.codeUnitAt(0));
print((s1+s2).codeUnits);

-----------------
输出结果:
hello 
world.
hello world.
65
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 46]



Some other languages ​​directly use 'A', or charCodeAt() and so on to get it.

Use of escape characters:

var s1 = 'hel\lo ';
print(s1);


var s2 = r'hel\lo ';
print(s2);

------------------
输出结果:
hello 
hel\lo 

It can be seen that this processing is somewhat similar to the way of PYTHON, and r is added directly in front.

Some operations on strings:

var s1 = 'hello';
print( s1.substring(0,2));
print( s1.split(''));
print(s1.toUpperCase());
print( s1[0]);

------------
输出结果
he
[h, e, l, l, o]
HELLO
h

For performance considerations, such as concatenating strings with StringBuffer:

var sb = new StringBuffer();
sb.write('hello');
sb.writeAll(['W', 'o', 'r','d'], '');
sb.writeAll(['!','!'], ' ');
print(sb.toString());

----------------
输出结果
helloWord! !

Is it a bit like StringBuffer in JAVA.

3: Boolean type

The Boolean type in Dart only has true or false, and there is no such representation as 0 or 1.

bool a;
print(a);
if (a){
  print(0);
}else{
    print(1);
}
----
输出结果:
先输出null后,然后报错

Why is this? Because in Dart, the uninitialized ones are all NULL, not false.

The following code:

var A = 'hahah';
if (A) {
 print('A');
}

var a = 1;
if (a) {
 print('A');
}

These two pieces of code will all report errors, why is this? Because in dart, A and a are objects and cannot be used as judgment conditions, which is very different from languages ​​such as JS and python.

Lists type

A bit similar to the list in python, when it is defined, it is directly var a=[1,2].

list add:

var list = [8,1,2,3]; 
list.add(4);       
print(list);

------
[8, 1, 2, 3, 4]

delete element from list

var list = [8,1,2,3]; 
list.add(4);       
print(list);

list.remove(2)
print(list);

list.remove(2);
print(list);


-----
[8, 1, 2, 3, 4]
[8, 1, 3, 4]
[8, 1, 3, 4]

可以看到即使元素2已结被删除了,它再次删除的时候,也不会报错。

list.removeAt(index), list.remove是删除指定的元素,removeAt(i)是删除索引为i的元素.
而这个list.removeRange(0, 2);删除索引的范围是0到2的元素(不包括索引为2).
list.removeLast()是删除最后一个元素。
而list.removeWhere(bool);需要写个函数判断来删除哪些元素,比如:

bool del(int i){
   if (i>5){
     return true;
   }
   return false;
}
removeWhere会直接删除大于5的元素。



 

list join operation

var list = [1,2,3,4]; //实例化一个list
var s= list.join('!~');
print(list);
print(s);

---------
[1, 2, 3, 4]
1!~2!~3!~4

You can see that after list.join, it must be assigned to a new variable to be effective.

sorting of list

list.sort();

list shuffle

list.shuffle();

interception of list

var list = [1,1,2,5,8,89,100]; //实例化一个list
var a=list.sublist(0,5);
print(a);
-----
[1, 1, 2, 5, 8]

filling of the list

var list = [1,1,2,5,8,89,100]; //实例化一个list
list.fillRange(0, 5,20);
print(list);

------
[20, 20, 20, 20, 20, 89, 100]

clearing the list

var list = [1,1,2,5,8,89,100]; //实例化一个list
list.clear();
print(list);
-----------
[]

Addition of two list arrays

var list = [1,1,2,5,8,89,100]; //实例化一个list
list.addAll([1,2]);
print(list);

------------
[1, 1, 2, 5, 8, 89, 100, 1, 2]

traversal of list

var list = [1,1,2,5]; //实例化一个list

for (final x in list) {
  print(x);
}
for (var i = 0; i < list.length; i++) {
  print(list[i]);
}
list.forEach((i) => print(i));


三种方式

 

The last MAP type is written separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325393441&siteId=291194637