Why is my CPU code slow?

A decision tree over measurements you can actually take. Each step names the counter that answers it; each ending names a hardware cause and how to confirm it before you act.

Step 1

Is the CPU doing work, or waiting?

The measurement that answers it

Read IPC (or its reciprocal, CPI) alongside total instructions retired. Low IPC means the core is stalling; high IPC with a long runtime means you are simply executing a great many instructions.