adding lines in C code using Python script
Question: I need to calculate the execution time of a loop in C code and for that i need
to write a python script that adds "gettimeofday" before and after the loop by
detecting the comments before and after the loop.

Here is the code:

    
    
    int main(int argc, char** argv) {
      int i,j;
      int M = argv[0][0] * 10000;
      int res = argc; 
    
      // loopId = 1; depth = 1; outermost
      for (i=0; i<M; i++) {
        // loopId = 2; depth = 2; innermost 
        for (j=0; j<M; j++) {
          res *= 7 % 71;
        }     
        // end loop (loopId = 2)
        // loopId = 3; depth = 2; innermost
        for (j=0; j<M; j++){ 
          res += 9 % 91;
        }
        // end loop (loopId = 3)
      }
      // end loop (loopId = 1)
    
      return res;
    }
    


Answer: 
    import sys, re
    
    expS=re.compile(r'\s*//\s*loopId = (\d+); depth = \d+; \w+')
    expE=re.compile(r'\s*//\s*end loop \(loopId = (\d+)\)')
    lines, varcnt = [], 0
    with open(sys.argv[1]) as f:
        for line in f:
            line = line.rstrip()
            lines += [ line ]
            m = re.match(expS, line)
            if m:
                varcnt += 1
                loopid = int(m.group(1))
                lines += [ 'gettimeofday(&tv[{}], 0);'.format((loopid-1)*2) ]
                continue
            m = re.match(expE, line)
            if m:
                loopid = int(m.group(1))
                sid, eid = (loopid-1)*2, (loopid-1)*2+1
                lines += [ 'gettimeofday(&tv[{}], 0);'.format(eid) ]
                lines += [ 'printf("Id {}: %ld\\n", tdiff_xxx(&tv[{}],&tv[{}]));'.format(
                    loopid, sid, eid) ]
    
    print '#include <sys/time.h>'
    print 'struct timeval tv[{}];'.format(varcnt*2)
    print 'long tdiff_xxx(struct timeval *t0, struct timeval *t1) {'
    print '  return (t1->tv_sec-t0->tv_sec)*1000000 + t1->tv_usec-t0->tv_usec;'
    print '}' 
    for l in lines: print l
    

