Reasonably recent Intel and AMD processors (and most other high performance processors) include Performance Monitoring Counter (PMC) registers. You can monitor quite a variety of different things, but probably the most relevant to the question at hand would be number of instructions retired.
Putting these to serious use can be a little tricky -- for example, there are circumstances under which they can undercount the number of actual instructions retired (e.g., if a System Management Interrupt happens while executing a halt instruction, some processors won't count the resume from system management instruction). For the purposes at hand, however, you'd probably just show what it had, and ignore the minor detail that it could sometimes be a little off.
It's worth noting, however, that this is counting assembly language instructions, which don't correspond directly to lines of code in a typical higher-level language. Converting between the two could be non-trivial -- even in C, it's not at all fixed (though a lot of lines will be close to 1:1) but in something like Perl, for example, the number of instructions for a line of code could not only be quite large, but could also vary (hugely) depending on the content of a string, if it was being used as a pattern.