I am designing an algorithm that solves a linear system using the QR factorization, and the matrices I am dealing with are sparse and very large ($6000 \times 6000$). In order to improve the efficiency of the algorithm, I am trying to exploit the sparsity of the matrix by finding its bandwidth, but I have to run through the matrix a lot of times to find it, and it is taking too long.
The main idea I am using to find the bandwidth is:
for each row, find the start(row) and end(row): these are the intervals in which the elements are different of $0$;
to find start(row): iterate from the beginning of the row until the element is not $0$;
to find end(row): iterate from the end of the row until the element is not $0$;
The problem is that I am running through many unnecessary $0$'s, but I can not figure out how to avoid this and guarantee a solid result. Thanks.