revise bug:update nperfect only when all the data have been read or it will lead to count repeatedly