Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to speed up a tally/binning operation that I have to run many times.

I have many lists of between 1 and 10 numbers from the range [1,5] and I want to Tally the number of each number and output that in a list.

randomdata = RandomInteger[{1, 5}, #] & /@ RandomInteger[{1, 10}, 100000];

Here is my initial uncompiled implementation using SparseArray

f = Normal@SparseArray[Rule @@@ (Tally@#), 5] &

This works, here is a sample:

f{5, 4, 4, 3, 5, 2, 4, 2, 5, 2}
output: {0, 3, 1, 3, 3}

I'm trying to speed this up with a compiled implementation.

fC = Compile[{{totally, _Integer, 1}}, 
  Module[{tally = Tally[totally], table = Table[0, {i, 5}]},
   table[[tally[[All, 1]]]] = tally[[All, 2]];
   table],
  CompilationTarget -> "C", RuntimeOptions -> "Speed", 
  RuntimeAttributes -> {Listable}, Parallelization -> True]

This works on the sample

fC[{5, 4, 4, 3, 5, 2, 4, 2, 5, 2}]
output: {0, 3, 1, 3, 3}

but if I do f[randomdata] the kernel crashes without an error message and I can't figure out what went wrong.

Alternatively, if all the inputs are the same length:

randomdata = RandomInteger[{1, 5}, {100000, 5}];
fC[randomdata]

This works...

It seems that the problem comes down to list over multiple arguments where each one has a different length.

see a simpler example

totalC = Compile[{{list, _Integer, 1}}, Total[list], 
  CompilationTarget -> "C", RuntimeOptions -> "Speed", 
  RuntimeAttributes -> {Listable}, Parallelization -> True]

This works: totalC /@ randomdata;

This crashes: totalC[randomdata];

share|improve this question
    
fC/@ randomdata seems to work, and f[randomdata] seems also not work on my computer. –  user0501 Apr 29 '13 at 19:56
7  
I verified and filed a minimal-example bug report. –  Daniel Lichtblau Apr 29 '13 at 20:06
1  
This has been fixed as of version 10.0.0. –  ilian May 6 at 21:01
    
@ilian did you want to post that as an answer with a minimal working example and I can accept it? –  s0rce May 6 at 22:51
    
Thanks @s0rce, I think the answer already posted is fine. By the way, this was the same bug as mathematica.stackexchange.com/questions/17966 –  ilian May 7 at 11:10

1 Answer 1

up vote 2 down vote accepted

According to a comment by @ilian, this has been fixed as of version 10.0.0. It certainly works in version 10.1, as we can see below:

$Version
(* 10.1.0  for Linux x86 (64-bit) (March 24, 2015) *)

fC[randomdata] == (fC /@ randomdata)
(* True *)

totalC /@ randomdata
totalC[randomdata]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.