Magnesium or Melatonin?

I have trouble falling asleep quickly, so I’m wondering if a supplement will help. Is one type better than the other? How long does it typically take for a supplement to really take effect and are they safe to take every night for an extended period of time?