Why does my julia code run so slowly? - performance

redim = 2;
# Loading data
iris_data = readdlm("iris_data.csv");
iris_target = readdlm("iris_target.csv");
# Center data
iris_data = broadcast(-, iris_data, mean(iris_data, 1));
n_data, n_dim = size(iris_data);
Sw = zeros(n_dim, n_dim);
Sb = zeros(n_dim, n_dim);
C = cov(iris_data);
classes = unique(iris_target);
for i=1:length(classes)
index = find(x -> x==classes[i], iris_target);
d = iris_data[index,:];
classcov = cov(d);
Sw += length(index) / n_data .* classcov;
end
Sb = C - Sw;
evals, evecs = eig(Sw, Sb);
w = evecs[:,1:redim];
new_data = iris_data * w;
This code just does LDA (linear discriminant analysis) for the iris_data.
Reduct the dimensions of the iris_data to 2.
It will takes about 4 seconds, but Python(numpy/scipy) only takes about 0.6 seconds.
Why?

This is from the first page, second paragraph of the introduction in the Julia Manual:
Because Julia’s compiler is different from the interpreters used for languages like Python or R, you may find that Julia’s performance is unintuitive at first. If you find that something is slow, we highly recommend reading through the Performance Tips section before trying anything else. Once you understand how Julia works, it’s easy to write code that’s nearly as fast as C.
Excerpt:
Avoid global variables
A global variable might have its value, and therefore its type, change at any point. This makes it difficult for the compiler to optimize code using global variables. Variables should be local, or passed as arguments to functions, whenever possible.
Any code that is performance critical or being benchmarked should be inside a function.
We find that global names are frequently constants, and declaring them as such greatly improves performance
Knowing that the script (all procedural top level code) style is so pervasive among many scientific computing users, I would recommend you to at least wrap the whole file inside a let expression for starters (let introduces a new local scope), ie:
let
redim = 2
# Loading data
iris_data = readdlm("iris_data.csv")
iris_target = readdlm("iris_target.csv")
# Center data
iris_data = broadcast(-, iris_data, mean(iris_data, 1))
n_data, n_dim = size(iris_data)
Sw = zeros(n_dim, n_dim)
Sb = zeros(n_dim, n_dim)
C = cov(iris_data)
classes = unique(iris_target)
for i=1:length(classes)
index = find(x -> x==classes[i], iris_target)
d = iris_data[index,:]
classcov = cov(d)
Sw += length(index) / n_data .* classcov
end
Sb = C - Sw
evals, evecs = eig(Sw, Sb)
w = evecs[:,1:redim]
new_data = iris_data * w
end
But I would also urge you to refactor that into small functions and then compose a main function that calls the rest, something like this, notice how this refactor makes your code general and reusable (and fast):
module LinearDiscriminantAnalysis
export load_data, center_data
"Returns data and target Matrices."
load_data(data_path, target_path) = (readdlm(data_path), readdlm(target_path))
function center_data(data, target)
data = broadcast(-, data, mean(data, 1))
n_data, n_dim = size(data)
Sw = zeros(n_dim, n_dim)
Sb = zeros(n_dim, n_dim)
C = cov(data)
classes = unique(target)
for i=1:length(classes)
index = find(x -> x==classes[i], target)
d = data[index,:]
classcov = cov(d)
Sw += length(index) / n_data .* classcov
end
Sb = C - Sw
evals, evecs = eig(Sw, Sb)
redim = 2
w = evecs[:,1:redim]
return data * w
end
end
using LinearDiscriminantAnalysis
function main()
iris_data, iris_target = load_data("iris_data.csv", "iris_target.csv")
result = center_data(iris_data, iris_target)
#show result
end
main()
Notes:
You don't need all those semicolons.
anonymous functions are currently slow but that will change in v0.5. You can use FastAnonymous for now, if performance is critical.
In summary read carefully and take into account all the performance tips.
main is just a name, it could be anything else you like.

Related

f# - Use common object without passing it everywhere

I have function compile which takes AST and produces string with assembly instructions. Inside it I create a StringBuilder object. Also inside compile there are a lot of local functions like emitIf, emitLet, etc.
The reason of defining local functions is that StringBuilder object doesn't have to be passed to emit functions as additional parameter. But I feel that this solution is somewhat awkward.
module Compile
let compile ast =
let sb = new StringBuilder()
let emitn s = sb.AppendLine s |> ignore
let emitfn f = Printf.kprintf emitn f
let emitLoadNumber (n:int) =
emitfn " mov $%d, %%rax" n
let rec emitExpr env si =
//...
and emitIf isTail env si cond th el =
//...
As another solution, I could create StringBuilder in the module scope and also move local function into module scope.
My question is: is there a better way to define a bunch of functions that use common object, like stream or StringBuilder, without passing it to each of the functions?
In your situation, I'd probably define a bunch of functions in the module that take an explicit StringBuilder parameter, and then use partial application to define a bunch of local functions that close over one specific StringBuilder instance. E.g.,
module Compile
let emitn sb s = sb.AppendLine s |> ignore
let emitfn sb f = Printf.kprintf (emitn sb) f
let emitLoadNumber sb (n:int) =
emitfn sb " mov $%d, %%rax" n
let compile ast =
let sb = new StringBuilder()
let emitn = emitn sb
let emitfn = emitfn sb
let emitLoadNumber = emitLoadNumber sb
let rec emitExpr env si =
//...
and emitIf isTail env si cond th el =
//...
Now inside the compile function and its subfunctions (emitExpr, emitIf, and so on) you can use emitn "foo" and it will add the string "foo\n" to the StringBuilder instance you created at the start of compile. (Which will be a different instance each time you call the function, of course). But you also have generally-useful versions of emitn, emitfn, etc. available, which makes unit-testing FAR easier.

Why this function return an (owned) value?

the code
from: Genie howto repeat a string N times as an string arrayGenie howto repeat a string N times as an string array
def repeatwithsep (e: string, n: int, separator: string): string
var elen = e.length;
var slen = separator.length;
var a = new StringBuilder.sized ((elen * n) + (slen * (n - 1)) + 1);
for var i = 0 to (n - 1)
if i != 0
a.append_len (separator, slen)
a.append_len (e, elen)
return (owned) a.str
var a is a local variable, when a goes out of scope, it will be destroyed.
why this function
return (owned) a.str
what is the difference between
return a.str
return (owned) a.str
what is the benefit of (owned)
return a.str will make a copy of the string using g_strdup, because by default the function result and the StringBuilder will both own a separate copy of the string after the (implicit) assignment.
Since the StringBuilder stored in a will go out of scope and it's copy will thus never be used again this is not desireable / efficient in this case.
Hence the solution is to pass ownership of the string from a.str to the result of the function using the (owned) directive.
BTW: You can easily find this out by compiling both versions with valac -C and comparing the generated C code:
- _tmp21_->str = NULL;
- result = _tmp22_;
+ _tmp23_ = g_strdup (_tmp22_);
+ result = _tmp23_;
(In this comparison the left side was return (owned) a.str and the right side was return a.str)
PS: This is documented in the ownership section of the Vala tutorial and also the corresponding part of the Genie tutorial.
I would also recommend the Reference Handling article.

ldap searchtype substring not working when value is an integer is less than one thousand

I search and users from active directory. My code is below:
List<DirectoryEntry> dirEntries = ActiveDirectoryActions.getListByQuery("(&(objectClass=user)(displayName~=*" + q + "*))");
for (int i = 0; i < dirEntries.Count; i++)
{
SiteSearchResult r = new SiteSearchResult();
r.title = dirEntries[i].Properties["displayName"].Value.ToString();
r.url = "/" + lang + "/directory/user/" + dirEntries[i].Properties["sAMAccountName"].Value.ToString();
r.content = dirEntries[i].Properties["title"].Value.ToString();
result.Add(r);
}
And it is getListByQuery() function
public static List<DirectoryEntry> getListByQuery(string q)
{
DirectorySearcher drSearch = new DirectorySearcher(rootEntry);
drSearch.Filter = "(distinguishedName=" + Config.xml().Root.Elements("active_directory").Elements("root_ou").Select(x => x.Value).FirstOrDefault().ToString() + ")";
DirectoryEntry searchRoot = drSearch.FindAll()[0].GetDirectoryEntry();
drSearch.SearchRoot = searchRoot;
drSearch.Filter = q;
List<DirectoryEntry> r = new List<DirectoryEntry>();
SearchResultCollection sr = drSearch.FindAll();
for (int i = 0; i < sr.Count; i++)
{
r.Add(sr[i].GetDirectoryEntry());
}
return r;
}
Everthing is ok on my local server. But gives error on global server when I search integer value. And that is interesting when the value less than 1000 (<1000) .
[NullReferenceException: Object reference not set to an instance of an
object.] Myproject.Controllers.SearchController.Index(String
lang, String q) in
D:\dotNET\Myproject\Myproject\Controllers\SearchController.cs:60
Help please.
I think there are few issues you need to check
1) Does it make sense to call DirectoryEntry.FindAll() two times? Instead of using DirectorySearcher(rootEntry) you could try to set
string ldapPath = "LDAP://" + Config.xml().Root.Elements("active_directory").Elements("root_ou").Select(x => x.Value).FirstOrDefault().ToString();
DirectoryEntry de = new DirectoryEntry(ldapPath);
DirectorySearcher desearch = new DirectorySearcher(de);
deSearch.Filter = ...
2) Approximate search ~= might be not compatible with a substring search (=*substring*), e.g. for me it does not work. So try to change to =*1000* (without ~)
3) FindAll() in both cases could return null, so you should check for null in both cases.
4) [MSDN]
Due to implementation restrictions, the SearchResultCollection class
cannot release all of its unmanaged resources when it is garbage
collected. To prevent a memory leak, you must call the Dispose method
when the SearchResultCollection object is no longer needed.
So you either need to call sr.Dispose or use a using Statement.

Accessing position information in a scala combinatorparser kills performance

I wrote a new combinator for my parser in scala.
Its a variation of the ^^ combinator, which passes position information on.
But accessing the position information of the input element really cost performance.
In my case parsing a big example need around 3 seconds without position information, with it needs over 30 seconds.
I wrote a runnable example where the runtime is about 50% more when accessing the position.
Why is that? How can I get a better runtime?
Example:
import scala.util.parsing.combinator.RegexParsers
import scala.util.parsing.combinator.Parsers
import scala.util.matching.Regex
import scala.language.implicitConversions
object FooParser extends RegexParsers with Parsers {
var withPosInfo = false
def b: Parser[String] = regexB("""[a-z]+""".r) ^^# { case (b, x) => b + " ::" + x.toString }
def regexB(p: Regex): BParser[String] = new BParser(regex(p))
class BParser[T](p: Parser[T]) {
def ^^#[U](f: ((Int, Int), T) => U): Parser[U] = Parser { in =>
val source = in.source
val offset = in.offset
val start = handleWhiteSpace(source, offset)
val inwo = in.drop(start - offset)
p(inwo) match {
case Success(t, in1) =>
{
var a = 3
var b = 4
if(withPosInfo)
{ // takes a lot of time
a = inwo.pos.line
b = inwo.pos.column
}
Success(f((a, b), t), in1)
}
case ns: NoSuccess => ns
}
}
}
def main(args: Array[String]) = {
val r = "foo"*50000000
var now = System.nanoTime
parseAll(b, r)
var us = (System.nanoTime - now) / 1000
println("without: %d us".format(us))
withPosInfo = true
now = System.nanoTime
parseAll(b, r)
us = (System.nanoTime - now) / 1000
println("with : %d us".format(us))
}
}
Output:
without: 2952496 us
with : 4591070 us
Unfortunately, I don't think you can use the same approach. The problem is that line numbers end up implemented by scala.util.parsing.input.OffsetPosition which builds a list of every line break every time it is created. So if it ends up with string input it will parse the entire thing on every call to pos (twice in your example). See the code for CharSequenceReader and OffsetPosition for more details.
There is one quick thing you can do to speed this up:
val ip = inwo.pos
a = ip.line
b = ip.column
to at least avoid creating pos twice. But that still leaves you with a lot of redundant work. I'm afraid to really solve the problem you'll have to build the index as in OffsetPosition yourself, just once, and then keep referring to it.
You could also file a bug report / make an enhancement request. This is not a very good way to implement the feature.

Reapeat/until help in lua

Hello I've been trying to get this code to work, I even cheated and added in the goal to my code and its still not accepting my answer, any suggestions?
-- Functions...
function p() -- For user imput..
print("Enter # and try to get the closest to it! (Valid range is 1-100)")
local var = tonumber(io.read())
if var == nil then
var = 0
end
return var
end
--Start main code..
-- Initialize the pseudo random number generator (I'm on windows...)
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- Setting goal
goal = math.random(1,100)
-- Guessing loop...
repeat
g = p()
print(g)
print(goal)
until g == Goal
print("YOU GUESSED THE GOAL!")
Replace the G by a lower case g.
until g == goal

Resources