faster implementation of sum ( for Codility test )

前端 未结 22 2097
鱼传尺愫
鱼传尺愫 2021-02-04 11:47

How can the following simple implementation of sum be faster?

private long sum( int [] a, int begin, int end ) {
    if( a == null   ) {
        ret         


        
22条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 12:34

    Just some thought, not sure if accessing the pointer directly be faster

        int* pStart = a + begin;
        int* pEnd = a + end;
        while (pStart != pEnd)
        {
            r += *pStart++;
        }
    

提交回复
热议问题