Skip to content
João Medeiros edited this page Apr 10, 2023 · 47 revisions

Performance Measure

C

SYNOPSIS
       #include <time.h>

       int clock_gettime(clockid_t clockid, struct timespec *tp);
struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};
#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    struct timespec a, b;
    unsigned int t;

    clock_gettime(CLOCK_MONOTONIC, &b);
    sleep(1);
    clock_gettime(CLOCK_MONOTONIC, &a);

    t = (a.tv_sec * 1e9 + a.tv_nsec) - (b.tv_sec * 1e9 + b.tv_nsec);

    printf("%u\n", t);

    return 0;
}
#include <stdio.h>
#include <time.h>

int search(int *v, unsigned int n, int k)
{
    while (n-- > 0)
    {
        if (v[n] == k)
            return n;
    }
    return -1;
}

int main(int argc, char **argv)
{
    struct timespec a, b;
    unsigned int t;
    
    unsigned int n = 10000;
    int i, v[n];

    for (i = 0; i < n; i++)
    {
        v[i] = i;
    }

    clock_gettime(CLOCK_MONOTONIC, &b);
    search(v, n, -1);
    clock_gettime(CLOCK_MONOTONIC, &a);

    t = (a.tv_sec * 1e9 + a.tv_nsec) - (b.tv_sec * 1e9 + b.tv_nsec);

    printf("%u\n", t);

    return 0;
}

Python

import time

if __name__ == "__main__":

    b = time.time_ns()
    time.sleep(1)
    a = time.time_ns()

    t = a - b

    print(t)
import time

def search(v, k):
    n = len(v)
    while (n > 0):
        n = n - 1
        if v[n] == k:
            return n
    return -1

if __name__ == "__main__":
    v = list(range(1000))

    b = time.time_ns()
    search(v, -1)
    a = time.time_ns()

    t = a - b

    print(t)

Java

import java.lang.System;
import java.lang.Thread;

public class Measure
{
    public static void main(String[] args) throws InterruptedException 
    {
        long a, b, t;

        b = java.lang.System.nanoTime();
        java.lang.Thread.sleep(1000);
        a = java.lang.System.nanoTime();

        t = (a - b);

        java.lang.System.out.println(t);
    }
}
import java.lang.System;

public class Measure
{
    public static int search(int[] v, int k)
    {
        int n = v.length;

        while (n-- > 0)
        {
            if (v[n] == k)
                return n;
        }

        return -1;
    }

    public static void main(String[] args) throws InterruptedException 
    {
        long a, b, t;
        int[] v = new int[10000];
        int i;

        for (i = 0; i < v.length; i++)
        {
            v[i] = i;
        }

        b = java.lang.System.nanoTime();
        search(v, -1);
        a = java.lang.System.nanoTime();

        t = (a - b);

        java.lang.System.out.println(t);
    }
}

Rust

use std::{thread, time};

fn main()
{
    let duration = time::Duration::from_secs(1);

    let now = time::Instant::now();
    thread::sleep(duration);
    let measure = now.elapsed();

    println!("{}", measure.as_nanos());
}
use std::time;

fn search(v: &mut [i32], k: i32) -> i32
{
    let mut n = v.len();
    while n > 0
    {
        n = n - 1;

        if v[n] == k
        {
            return n as i32;
        }
    }

    return -1;
}

fn main()
{
    const N: usize = 10000;
    let mut v: [i32; N] = [0; N];

    for i in 0..(N - 1)
    {
        v[i] = i as i32;
    }

    let now = time::Instant::now();
    search(&mut v, -1);
    let measure = now.elapsed();

    println!("{}", measure.as_nanos());
}

Comparação

Comparativo.

Clone this wiki locally