The primary difference between a compiler and an interpreter lies in when they perform their tasks. A compiler translates the entire source code into machine code during compilation time, while an interpreter translates and executes the source code line by line during execution time.