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

performance of cat of scalars of mixed types #4467

Closed
rwgardner opened this issue Oct 10, 2013 · 5 comments
Closed

performance of cat of scalars of mixed types #4467

rwgardner opened this issue Oct 10, 2013 · 5 comments
Assignees
Labels
performance Must go faster

Comments

@rwgardner
Copy link

The performance of

ar = [a, b, c, d, e]

is much worse than the performance of

ar = Array(Float64, 5)
ar[1]=a
ar[2]=b
ar[3]=c
ar[4]=d
ar[5]=e

where a-e are Float64's because the former syntax uses the cat operation (for a good reason, as explained by @JeffBezanson - a-e could be many things including arrays etc., which makes the "bracket" syntax require an extremely dynamic operation (i.e., cat) in the general case).

This is a request to analyze constructs in the "bracket" notation (i.e., the first example above) and optimize the creation of the array in cases where the types of the elements can be determined statically. For example, translate the first block of code to the second internally, or do something more optimal.

(Changing the [ ] code to manually declare and assign arrays as above in our code increases speed by about 25% currently. However, the syntax is undesirable in our context from a readability standpoint.)

@kmsquire
Copy link
Member

This does not address the request per se, but an alternate syntax is

ar = Float64[a, b, c, d, e]

How does the performance of this compare with your second code block?

@rwgardner
Copy link
Author

Ah, thanks. ( @JeffBezanson had actually mentioned something like that too, which I forgot.)

The performance is nearly the same as the second block of code (maybe 1-2% slower, although my tests were limited so this is probably like 2% plus or minus 3%.)

That syntax is much nicer than my second block of code, but I'd still request optimization of the other case as well.

@JeffBezanson
Copy link
Member

I started looking at this. If a,b,c,d,e are all the same type, then the two versions ([] vs. T[]) are the same, but if they're not then T[] is drastically faster. I can close much of this particular gap.

@rwgardner
Copy link
Author

Ah that makes sense. One of the variables was an integer.

@ghost ghost assigned JeffBezanson Oct 26, 2013
@JeffBezanson JeffBezanson changed the title performance of cat of scalars performance of cat of scalars of mixed types Feb 20, 2015
JeffBezanson added a commit that referenced this issue Feb 20, 2015
will not have full effect until _oldstyle_array_vcat_ is turned off.
@KristofferC
Copy link
Member

These now both take the same time (30 ns) using the function below

function f()
	a, b, c, d, e = 1.0, 2.0, 3.0, 4.0, 5.0
	println(@benchmark ar = [$a, $b, $c, $d, $e])
	println(@benchmark begin ar = Array{Float64}(5)
	ar[1]=$a
	ar[2]=$b
	ar[3]=$c
	ar[4]=$d
	ar[5]=$e
	end)
end

@tkelman tkelman added the potential benchmark Could make a good benchmark in BaseBenchmarks label Jan 21, 2017
@KristofferC KristofferC removed the potential benchmark Could make a good benchmark in BaseBenchmarks label Oct 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

No branches or pull requests

5 participants