Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: count number of 1's in the matrix
Date: Tue, 30 Jun 2009 22:25:03 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 41
Message-ID: <h2e3bu$9hk$1@fred.mathworks.com>
References: <h2dspt$pgv$1@fred.mathworks.com> <op.uwcwssi0a5ziv5@uthamaa.dhcp.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246400703 9780 172.30.248.35 (30 Jun 2009 22:25:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 30 Jun 2009 22:25:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:551856


>> d=double(rand(5010)>.5);
>> timeit(@()nnz(d))

ans =

    0.1583

>> timeit(@()sum(d(:)))

ans =

    0.0408

>> timeit(@()sum(sum(d)))

ans =

    0.0409

>> 

>> d=sparse(d);
>> timeit(@()nnz(d))

ans =

  2.0785e-005

>> timeit(@()sum(d(:)))

ans =

    0.2306

>> timeit(@()sum(sum(d)))

ans =

    0.0209

% Bruno