On Tue, Dec 1, 2015 at 11:07 PM, Jorge Johnson <jorge.john...@gmail.com> wrote:
> Hi,
> I made my first julia function.
> I build two functional identical functions, the first one using types and
> the second one without types.
>
> I was expecting the typed function to be more efficient in time and memory
> than the untyped one.
> however, I was surprised. The untyped function runs very fast and with less
> memory.
>
> here the code an times:

No, adding random type constraint won't make the code runs faster as
long as the compiler can figure out what the type is. The actual
runtime is very short so the total time you are measuring is very
likely the compilation time and not the runtime. The first one might
take longer because of the compilation of the functions used by the
`@time` macro.

>
> #TYPED
> function HowManyNucleotides(dnaSequenceFile::Base.ASCIIString)
> Tuple{Int,Int,Int,Int}
>     nucA::Int = 0
>     nucG::Int = 0
>     nucC::Int = 0
>     nucT::Int = 0
>     nucleotyde::Char = 0
>     f = open(dnaSequenceFile)
>     while (!eof(f))
>       nucleotyde = read(f,Char)
>       if isAdenine(nucleotyde)
>           nucA+=1
>       elseif isGuanine(nucleotyde)
>           nucG+=1
>       elseif isCytosine(nucleotyde)
>           nucC+=1
>       elseif isThymine(nucleotyde)
>           nucT+=1
>       end
>     end
>     flush(f)
>     close(f)
>     [nucA,nucG,nucC,nucT]
> end
>
>
>
>
>
>
> #UNTYPED
> function HowManyNucleo(dnaSequenceFile)
>     nucA = 0
>     nucG = 0
>     nucC = 0
>     nucT = 0
>     nucleotyde = '.'
>     f = open(dnaSequenceFile)
>     while (!eof(f))
>       nucleotyde = read(f,Char)
>       if isAdenine(nucleotyde)
>           nucA+=1
>       elseif isGuanine(nucleotyde)
>           nucG+=1
>       elseif isCytosine(nucleotyde)
>           nucC+=1
>       elseif isThymine(nucleotyde)
>           nucT+=1
>       end
>     end
>     flush(f)
>     close(f)
>     [nucA,nucG,nucC,nucT]
> end
>
>
> Times of calls are respectively:
>
>
> #TYPED
>
> 0.011514 seconds (26.61 k allocations: 974.385 KB)
>
>
> #UNTYPED
>
> 0.005221 seconds (4.88 k allocations: 255.773 KB)
>
>
> I´m confused, because it is supposed typed julia is more fast.
>
>
> Any Ideas?
>
> Thank you

Reply via email to