Commit d08d5ac2 authored by Riccardo La Placa's avatar Riccardo La Placa
Browse files

Added fastcounts, which much faster than histcounts especially for sorted data

parent 01513018
Loading
Loading
Loading
Loading

functions/fastcounts.m

0 → 100644
+41 −0
Original line number Diff line number Diff line
function counts = fastcounts(times,tedges)
%FASTCOUNTS Histogram of array of times put in bins with edges tedges
%   For longish arrays it's much faster than hisctounts and uses only a
%   fraction of its memory; operating on sorted times are where it shines
%   but it still is faster in  unsorted ones. 
%   The tedges can be non-uniformly spaced but the algorithm doesn't
%   rescale counts based on width: if you want that use histcounts

if (~issorted(times))
    times = sort(times);
end
Nedges = length(tedges);
Nbins = length(tedges)-1;
Nphot = length(times);
counts = zeros(size(tedges));
counts = counts(1:Nbins);
% It appears matlab is faster with doubles anyway, we can take the
% negligible hit in memory
% counts = zeros(size(tedges),'int16');
% counts = counts(1:Nbins);
% if (Nphot<intmax('int16'))
%     counts = zeros(size(counts),'int16');
% elseif (Nphot<intmax('int32'))
%     counts = zeros(size(counts),'int32');
% elseif (Nphot<intmax('int64'))
%     counts = zeros(size(counts),'int64');
% else
%     disp(['The length of times is greater than ',num2str(intmax('int64')),', nothing will work!'])
% end

j = 1;
i = 1;
while i < Nedges
    while (j<=Nphot && times(j)<tedges(i+1))
        counts(i) = counts(i)+1;
        j = j+1;
    end
    i = i+1;
end

end
 No newline at end of file