Programming notes

From raju

monitor memory usage

To monitor the memory usage of long running applications, we can use top in batch mode.

Approach:
Start the application in background, monitor the pid of interest at regular intervals. For example, the command below will run top on a single process, every 10 seconds, for a maximum of 1080 times (three hours).

     % cat cmd.sh
    #! /bin/env zsh
    
    command of interest >& log.txt &
    PID=$!
    top -p $PID -b -n 1080 -d 10 >& top.txt &
    

The output from the command is redirected to log.txt and that from top is redirected to top.txt

If necessary, change the top command as needed. For example,

    top -u raju -b -n 1080 -d 10 -c >& top.out &
    

will monitor all the processes from a user named 'raju'.

See also: http://www.dedoimedo.com/computers/linux-cool-hacks.html

indexing schemes

  • R, octave, Matlab use a 1-based indexing scheme. i.e. the first element of a vector has an index of 1.
  • Perl, C, C++ use a 0-based indexing scheme.
  • Fortran 90 - arrays can start with an arbitrary index.

Style guides

exponentiation

language exponentiation operator
python, perl **
R both **, ^ will work
C, C++ pow function in the math library

For a comprehensive list, see https://en.wikipedia.org/wiki/Exponentiation#In_programming_languages

Tips on programming

  • Do not focus on where the code is wrong, but only on why the code is wrong. Use asserts to enforce the assumptions about the algorithm. They are helpful in catching the logic errors and in documenting the code.
  • Instead of calling the same function twice, create a variable to store the return value and reuse that. Variables are cheap compared to the time spent on evaluating a function.
  • Make code as modular as possible

handling bugs from users