Skip to content
This repository has been archived by the owner on Jul 26, 2020. It is now read-only.

Performance no acceptable #32

Closed
thomaskilian opened this issue Nov 14, 2016 · 4 comments
Closed

Performance no acceptable #32

thomaskilian opened this issue Nov 14, 2016 · 4 comments

Comments

@thomaskilian
Copy link
Contributor

thomaskilian commented Nov 14, 2016

Running this little Python snippet on my machine takes 3.5 seconds:

import numpy as np
import time

t = time.time()
for i in range(1000):
    a = np.arange(100000)
    a += 1
    a = a * a
    a = a / a
    b = np.ones(100000)
    if not np.allclose(a, b): print "?"
print time.time() - t

The corresponding Swix-variant takes 33.7 seconds (factor 10 slower than NumPy).

@thomaskilian
Copy link
Contributor Author

I made a quick check

public func test(res:inout Vector) {
  vDSP_vsaddD(!res, 1, !res, !res, 1, res.n.length)
  vDSP_vmulD(!res, 1, !res, 1, !res, 1, res.n.length)
  vDSP_vdivD(!res, 1, !res, 1, !res, 1, res.n.length)
}

In the same loop takes 5.2 seconds. Still slower than NumPy, but clearly showing that Swift is not really useable for vector operations. At least not the "Swift"-way.

@stsievert
Copy link
Owner

public func test(res:inout Vector)

Sounds like we should redefine +=, *= and the like to incorporate this.

speed tests

I did provide some speed comparisons at http://scottsievert.com/swix/speed.html

These can be re-run with swixSpeedTests(). This can be compared with Python/Julia/Matlab by running the scripts in https://github.com/stsievert/swix/tree/master/swix/speed

@stsievert
Copy link
Owner

Also we should verify that the compile time flags are set proper (more detail on the speed page).

@thomaskilian
Copy link
Contributor Author

thomaskilian commented Nov 14, 2016

I have coded my test in C++ (honestly with quite some optimization, but that's what I'm really after)

// Definition: CPP.cpp
#include "CPP.hpp"
#include <Accelerate/Accelerate.h>
#include <iostream>
using namespace std;
const int size = 100000;
void CPP::calc() {
  memcpy(vector, ones, size*sizeof *ones);
  vDSP_vsaddD(vector, 1, vector, vector, 1, size);
  vDSP_vmulD(vector, 1, vector, 1, vector, 1, size);
  vDSP_vdivD(vector, 1, vector, 1, vector, 1, size);
  vDSP_vsubD(vector, 1, ones, 1, vector, 1, size);
  vDSP_vabsD(vector, 1, vector, 1, size);
  double m = 0;
  vDSP_maxvD(vector, 1, &m, size);
  if (abs(m) > 1e-6) { printf("argh"); }
}

and it took 0.07 seconds. So for me the answer is clear. Swift is no good choice for such things. The language does not offer enough freedom to implement this kind of operations. It's simply a knock out. You can use it for children's math (if you have matrices with a few dozen entries), but for large problems (I'm targeting towards neural networks), there is no way to get Swift to work. Note that I already have tested the above with optimization.

Btw. Your speed.swift has not been merged. It would not compile.

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

No branches or pull requests

2 participants