I'll talk about the looping instead of how to optimize it performance-wise (generating chunks with canvas or paster is a good idea, you will end up with about two big sprites that you will move at the bottom at any time).
If you wanna know the height value at a specific x, you can look up:
height = array.at(xvalue%array.width)
So if you wanna know the height at x = 4000, you will end up looking in the array at 399. The operation is called modulo.