Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the Go result correct? #2

Closed
xeoncross opened this issue Feb 2, 2018 · 3 comments
Closed

Is the Go result correct? #2

xeoncross opened this issue Feb 2, 2018 · 3 comments

Comments

@xeoncross
Copy link

xeoncross commented Feb 2, 2018

The results from your test showed go was really slow. For simplicity I combined everything into it's own file. I'm getting a result of 110 ms (vs 37879). I may be misunderstanding something.

package main

/*
#include <stdio.h>
#include <sys/time.h>

int plus(int x, int y)
{
    return x + y;
}

int plusone(int x)
{
    return x + 1;
}

long long current_timestamp()
{
    struct timeval te; 
    gettimeofday(&te, NULL); // get current time
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
    
    return milliseconds;
}
*/
import "C"
import "fmt"
import "os"
import "strconv"

func run(count C.int) {
    start := C.current_timestamp()
    
    var x C.int = 0
    for x < count {
        x = C.plusone(x)
    }
    
    fmt.Println(C.current_timestamp() - start)
}

func main() {
    if (len(os.Args) == 1) {
        fmt.Println("First arg (0 - 2000000000) is required.")
        return
    }
    
    count, err := strconv.Atoi(os.Args[1])
    if err != nil || count <= 0 || count > 2000000000 {
        fmt.Println("Must be a positive number not exceeding 2 billion.");
        return
    }
    
    // load
    C.plusone(C.int(C.current_timestamp()))
    
    // start
    run(C.int(count))
}

go version go1.9.2 darwin/amd64

Core i5 Mac

@jackmott
Copy link

jackmott commented Feb 2, 2018

Did you use 500 million or 1 million? His test results used 500 million.
Also he used Go 1.8, it would be interesting to see if you can run it with both, to see if things have improved.

@xeoncross
Copy link
Author

You are correct. I didn't pay close enough attention to the second run-all (500000000). Running it again (compared to the C baseline) I get.

# Go
./app 500000000
53058

# C
./a.out 500000000
1989

I have no idea how LuaJit could be faster than C. I assume the compiler is doing something fishy.

Here is my baseline C version

#include <stdio.h>
#include <sys/time.h>

int plus(int x, int y)
{
    return x + y;
}

int plusone(int x)
{
    return x + 1;
}

long long current_timestamp()
{
    struct timeval te; 
    gettimeofday(&te, NULL); // get current time
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
    
    return milliseconds;
}

void run(int count)
{
    long long start = current_timestamp();
    long long end;
    
    int x = 0;
    while (x < count)
        x = plusone(x);
    
    printf("%lld\n", current_timestamp() - start);
}

int main(int argc, char** argv)
{
    if (argc == 1) {
        printf("First arg is required \n");
        return 1;
    }
    
    int count = atoi(argv[1]);
    if (count <= 0 || count > 2000000000) {
        printf("Must be a positive number not exceeding 2 billion.\n");
        return 1;
    }
    
    // start immediately
    run(count);

    return 0;
}

@masklinn
Copy link

masklinn commented Jul 18, 2018

@xeoncross

I have no idea how LuaJit could be faster than C. I assume the compiler is doing something fishy.

It's not doing anything fishy, it's leveraging JITing to skip the PLT indirection.

You could get similar performances in C by using dlsym to get a direct handle to the function or compiling with -fno-plt. But given that's neither the default nor habitual I think the PLT-using version makes sense and is more honest, it's what most people would effectively get.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants