Simple comparison between Python and Julia

Python is a simple and easy-to-use language. At present, it ranks first in the popular list of tiobe programming languages. Due to its simple language and good interaction with the ecology of C, python has become the first language of artificial intelligence. Due to the existence of GIL, python cannot use multithreading, and Python's speed has been criticized.

Rust is an emerging modern language. It is a static language. It focuses on type security, memory security, zero cost abstraction. Trust has no garbage collection mechanism, which makes the speed of rust comparable to that of C + +. The unique concept of ownership and life cycle in rust is a clever paradigm transformation, but it also increases the difficulty of learning rust.

Julia is a young language dominated by MIT. It is mainly oriented to scientific computing and aims to provide simple syntax and high efficiency. As Julia is mainly oriented to data scientists and academia, its language design is different from other programming languages. At present, Julia still lacks ecology compared with Python.

This time is a very simple comparison. Use three languages to complete a task, sort the elements in an array, and put the element with value 0 at the end of the array, such as input [0,5,4,8,3,1,0] and output [1,3,4,5,8,0,0]

Refer to the topic on leetcode Force buckle

Next, let's look at the Python code

def move_zeros(li):
    a = []
    b = []
    res = []
    for i in li:
        if i == 0:
            a.append(i)
        else:
            b.append(i)

    new = []
    for i in range(len(b)):
        smallest = findSmallest(b)
        new.append(b.pop(smallest))
    
    new.extend(a)
    return new

# Find the location of the smallest element
def findSmallest(arr):
    smallest = arr[0]
    smallest_index = 0
    for i in range(1, len(arr)):
        if arr[i] < smallest:
            smallest = arr[i]
            smallest_index = i
    return smallest_index

T rust version

fn main() {
    let orin = vec![0, 9, 8, 0, 1, 7];
    let res: (Vec<i32>, Vec<i32>) = split_zero(&orin);
    let (mut x, y) = res;
	
    let mut new: Vec<i32> = Vec::new();
    for _i in 0..x.len() {
        let smallest = find_smallest(&x);
        new.push(x.remove(smallest))
    };
    
    //Add 0 to end
    for i in y {
        new.push(i);
    };
    println!("{:?}", new);
}

//Find the position of the minimum value in the vector
fn find_smallest(arr: &Vec<i32>) -> usize {
    let mut smallest = arr[0];
    let mut smallest_index = 0;
    for i in 1..arr.len() {
        if arr[i] < smallest {
            smallest = arr[i];
            smallest_index = i;
        }
    }
    return smallest_index
}

//Divide the zero and non-zero vectors into two vectors
fn split_zero(li: &Vec<i32>) -> (Vec<i32>, Vec<i32>) {
    let mut a: Vec<i32> = Vec::new();
    let mut b: Vec<i32> = Vec::new();
    for i in li {
        if *i != 0 {
            a.push(*i)
        }
        else {
            b.push(*i)
        }
    }
    return (a, b)
}

Julia's Version (CSDN does not support Julia's highlighting)

# Separates non-zero and zero of the list into two arrays
function split(list)
    a = []
    b = []
    for i in list
        if i != 0
            push!(a, i)
        else
            push!(b, i)
        end
    end
    return a, b
end

# Find the position of the smallest element of the array
function smallest(list)
    smallest = list[1]
    smallest_index = 1
    for num in [i for i in 2:length(list)]
        if list[num] < smallest
            smallest = list[num]
            smallest_index = num
        end
    end
    return smallest_index
end

orin = [0,2,8,1,6,0]
x, y = split(orin)
new = []
for i in [i for i in 1:length(x)]
    small = smallest(x)
    push!(new, popat!(x, small))
end
new.append!(new, y)
println(new)

Personally, Julia's syntax is the simplest. Julia uses arrays in the same way as python. The index of the first element of Julia's array is 1, which is different from Python and trust. In terms of control statements, the three are basically the same. In Python, indent is used to identify the block of code, curly braces are used in trust, and end is used in Julia. Since both Python and Julia are dynamically typed languages, the trust code is much simpler than that of static languages.

Conclusion: my feeling is that if you want to develop faster, Python is the first choice, because Python has a large number of libraries and a large number of users in China, so it is easy to find tutorial materials. Julia would be a better choice if it is a large amount of data calculation, especially matrix operation. It involves a lower level development choice: trust

Keywords: Python Rust julia

Added by gerkintrigg on Mon, 14 Feb 2022 03:12:16 +0200