H
H
Hellpain2014-11-08 23:06:42
Profiling
Hellpain, 2014-11-08 23:06:42

What profilers exist for rust programs?

Guys, who has experience in profiling programs on rust? I didn’t find any tools through Google, I can only measure time through println! do what is inconvenient. I rewrite the program (number grinder, I find all prime numbers up to n) from python to rust - as a result, 1.5 times slower, although I calculated 10-50 times faster.
Python code (4.7 s on which computer):

# coding=utf-8
from time import time

def get_primes_below2(n):
    """ Returns  a list of primes < n """
    sieve = [True] * n
    q = int(n**0.5)+1
    for i in xrange(3, q, 2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    result = [2] + [i for i in xrange(3,n,2) if sieve[i]]
    return result

t = time()
A = 10 ** 8
p = get_primes_below2(A)
print 'result = {}, time = {}'.format(1, time() - t)

Code on rust (10.3s on my computer):
extern crate time;

use std::num;
use time::now;

fn get_primes_below(n: uint) -> Vec<uint> {
    let mut sieve:Vec<bool> = Vec::from_fn(n, |_| {true});
    let stop = sieve.len();
    let hight_border =  ((n as f64).sqrt() + 1f64) as uint;
    println!("{}", hight_border);
    for i in range(3, hight_border).filter(|j| { j % 2 != 0 }) {
        //if i % 2 == 0 {continue;}
        if sieve[i] == true {
            let start = i * i;
            let step = 2 * i;
            let mut j = start;
            while j < stop  {
                sieve[j] = false;
                j += step;
            }
        }
    }

    let mut result:Vec<uint> = Vec::with_capacity(n);
    result.push(2);
    for i in range(3, n).filter(|j| { j % 2 != 0 }) {
        //if i % 2 == 0 {continue;}
        if sieve[i] == true {result.push(i)};
    }
    result
}

fn main() {
    let start_time = now();
    let a: uint = num::pow(10, 8);
    let primes = get_primes_below(a);
    let stop_time = now();
    println!("{}.{}", (stop_time.tm_sec - start_time.tm_sec), (stop_time.tm_nsec - start_time.tm_nsec) / 1000 / 1000);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
obvn, 2014-11-26
@obvn

Almost any profiler for C will work.
The output is a regular binary code, and if it contains debug information, it will be profiled by anything.
More specifically: perf, gprof, XCode Instruments (on OS X) will work.
There is also such a note about Rust developer tools, you can find a lot of interesting and useful things.
As for the task of generating prime numbers, I think it's better to implement the Iterator trait. This will allow you to lazily receive sequences from the iterator, or immediately take the desired one by count. An example is here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question