Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread Christoph Ortner
That is useful to know - thanks.


Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread Stefan Karpinski
You can declare them outside the loop with the local keyword – that a better 
option and less likely to lead to type inference issues.

> On Sep 12, 2014, at 11:59 PM, Christoph Ortner  
> wrote:
> 
> 
> The point was to be able to access d1, d2 outside the loop to check their 
> types.
> 


Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread Christoph Ortner

The point was to be able to access d1, d2 outside the loop to check their 
types.



Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread Kevin Squire
On Friday, September 12, 2014, Douglas Bates  wrote:

> On Friday, September 12, 2014 12:41:49 AM UTC-5, Christoph Ortner wrote:
>>
>> That did work - thank you, see code below.  To explain: this came from a
>> bottleneck in a bigger code, so my problem there must be a different one.
>>   -- Christoph
>>
>> function testtime()
>> a1 = rand(10, 10, 100, 100)
>> b1 = rand(10, 100, 100)
>> c1 = rand(10, 100, 100)
>> d1 = []
>> const a2 = rand(10,10,100,100)
>> const b2 = rand(10,100,100)
>> const c2 = rand(10,100,100)
>> d2 = []
>> @time(begin
>> for n = 1:10
>> d1 = [ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10, b
>> = 1:10, i=1:100,j=1:100 ]
>> end
>> end)
>> println(typeof(d1))
>> @time(begin
>> for n = 1:10
>> d2 = [ a2[a,b,i,j] .* b2[a,i,j] .* c2[b,i,j] for a = 1:10, b
>> = 1:10, i=1:100,j=1:100 ]
>> end
>> end)
>> println(typeof(d2))
>> end
>>
>
> A small point about the code. The initialization
>
> d1 = []
>
> is unnecessary.  In fact, this assignment initializes d1 to a
> 1-dimensional array with 0 elements.
>
> The comprehension creates the array of the correct dimension and populates
> it so that initial 1-dimensional array is discarded.
>

Even more, [ ] creates an empty array with element type None, so it can't
even be extended!


Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread Douglas Bates
On Friday, September 12, 2014 12:41:49 AM UTC-5, Christoph Ortner wrote:
>
> That did work - thank you, see code below.  To explain: this came from a 
> bottleneck in a bigger code, so my problem there must be a different one.
>   -- Christoph
>
> function testtime()
> a1 = rand(10, 10, 100, 100)
> b1 = rand(10, 100, 100)
> c1 = rand(10, 100, 100)
> d1 = []
> const a2 = rand(10,10,100,100)
> const b2 = rand(10,100,100)
> const c2 = rand(10,100,100)
> d2 = []
> @time(begin
> for n = 1:10
> d1 = [ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10, b = 
> 1:10, i=1:100,j=1:100 ]
> end
> end)
> println(typeof(d1))
> @time(begin
> for n = 1:10
> d2 = [ a2[a,b,i,j] .* b2[a,i,j] .* c2[b,i,j] for a = 1:10, b = 
> 1:10, i=1:100,j=1:100 ]
> end
> end)
> println(typeof(d2))
> end
>

A small point about the code. The initialization 

d1 = []

is unnecessary.  In fact, this assignment initializes d1 to a 1-dimensional 
array with 0 elements.

The comprehension creates the array of the correct dimension and populates 
it so that initial 1-dimensional array is discarded.
 

>
> testtime()
>
>
>
>

Re: [julia-users] Comprehension Produces Any

2014-09-12 Thread gael . mcdon

>
> @time(begin
> for n = 1:10
> d1 = Float64[ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10, 
> b = 1:10, i=1:100,j=1:100 ]
> end
> end)
>

For the sake of completeness,
begin
...
end 
blocks are not local.

I thought let blocks would but it appears they don't.


Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread Christoph Ortner
And here the OUTPUT:

elapsed time: 0.110285914 seconds (80001120 bytes allocated, 59.36% gc time)
Array{Float64,4}
elapsed time: 0.079318859 seconds (80001120 bytes allocated, 43.25% gc time)
Array{Float64,4}




Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread Christoph Ortner
That did work - thank you, see code below.  To explain: this came from a 
bottleneck in a bigger code, so my problem there must be a different one.
  -- Christoph

function testtime()
a1 = rand(10, 10, 100, 100)
b1 = rand(10, 100, 100)
c1 = rand(10, 100, 100)
d1 = []
const a2 = rand(10,10,100,100)
const b2 = rand(10,100,100)
const c2 = rand(10,100,100)
d2 = []
@time(begin
for n = 1:10
d1 = [ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10, b = 
1:10, i=1:100,j=1:100 ]
end
end)
println(typeof(d1))
@time(begin
for n = 1:10
d2 = [ a2[a,b,i,j] .* b2[a,i,j] .* c2[b,i,j] for a = 1:10, b = 
1:10, i=1:100,j=1:100 ]
end
end)
println(typeof(d2))
end

testtime()





Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread Jameson Nash
You are still trying to run in global scope. Put your code in a function
before drawing conclusions.


On Fri, Sep 12, 2014 at 12:58 AM, Christoph Ortner <
christophortn...@gmail.com> wrote:

> On Friday, 12 September 2014 02:24:15 UTC+1, gael@gmail.com wrote:
>
>> Wouldn't it be enough to put it in a local scope (let block or in a
>> function?).
>>
>> For more information, you can ask or look at the Performance tips part of
>> the manual.
>>
> I'd be interested in. Here is another code block:
>
> a1 = rand(10, 10, 100, 100)
> b1 = rand(10, 100, 100)
> c1 = rand(10, 100, 100)
> const a2 = rand(10,10,100,100)
> const b2 = rand(10,100,100)
> const c2 = rand(10,100,100)
> @time(begin
> for n = 1:10
> d1 = Float64[ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10,
> b = 1:10, i=1:100,j=1:100 ]
> end
> end)
> @time(begin
> for n = 1:10
> d2 = [ a2[a,b,i,j] .* b2[a,i,j] .* c2[b,i,j] for a = 1:10, b =
> 1:10, i=1:100,j=1:100 ]
> end
> end)
>
>
> OUTPUT:
>
> elapsed time: 4.554876933 seconds (1039919360 bytes allocated, 25.73% gc time)
> elapsed time: 0.147784029 seconds (80001120 bytes allocated, 54.91% gc time)
>
> This is a factor-30 slow-down. I think comprehensions are wonderful to
> read, but this makes them useless, unless there is a reasonably elegant fix.
>
>


Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread Christoph Ortner
On Friday, 12 September 2014 02:24:15 UTC+1, gael@gmail.com wrote:

> Wouldn't it be enough to put it in a local scope (let block or in a 
> function?).
>
> For more information, you can ask or look at the Performance tips part of 
> the manual.
>
I'd be interested in. Here is another code block:

a1 = rand(10, 10, 100, 100)
b1 = rand(10, 100, 100)
c1 = rand(10, 100, 100)
const a2 = rand(10,10,100,100)
const b2 = rand(10,100,100)
const c2 = rand(10,100,100)
@time(begin
for n = 1:10
d1 = Float64[ a1[a,b,i,j] .* b1[a,i,j] .* c1[b,i,j] for a = 1:10, b 
= 1:10, i=1:100,j=1:100 ]
end
end)
@time(begin
for n = 1:10
d2 = [ a2[a,b,i,j] .* b2[a,i,j] .* c2[b,i,j] for a = 1:10, b = 
1:10, i=1:100,j=1:100 ]
end
end)


OUTPUT:

elapsed time: 4.554876933 seconds (1039919360 bytes allocated, 25.73% gc time)
elapsed time: 0.147784029 seconds (80001120 bytes allocated, 54.91% gc time)

This is a factor-30 slow-down. I think comprehensions are wonderful to 
read, but this makes them useless, unless there is a reasonably elegant fix.



Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread gael . mcdon
Wouldn't it be enough to put it in a local scope (let block or in a function?).

For more information, you can ask or look at the Performance tips part of the 
manual.


Re: [julia-users] Comprehension Produces Any

2014-09-11 Thread Jiahao Chen
You may be interested in issue #7258
 and the julia-dev thread
linked to in
there.

Thanks,

Jiahao Chen
Staff Research Scientist
MIT Computer Science and Artificial Intelligence Laboratory

On Thu, Sep 11, 2014 at 3:40 PM, Christoph Ortner <
christophortn...@gmail.com> wrote:

>
> Here is a short code snippet, that got me puzzled.
> [Julia Version 0.3.0, Commit 7681878* (2014-08-20 20:43 UTC), Darwin
> (x86_64-apple-darwin13.3.0)]
>
>
>
> a = rand(3,3)
>
> b = rand(3,3)
>
> println(typeof(
>
> [a[i,j]*b[i,j] for i = 1:3, j=1:3]))
>
> println(typeof(a .* b))
>
> Array{Any,2}
> Array{Float64,2}
>
>
>
> Should Julia not create the same output in both cases? Is this a bug or
> intended?
>
>
>  --Christoph
>
> Julia Version 0.3.0
> Commit 7681878* (2014-08-20 20:43 UTC)
> Platform Info:
>   System: Darwin (x86_64-apple-darwin13.3.0)
>
>
>
>