code: def count_generator(generator, memory_efficient=True):
      
      if memory_efficient:
          counter = 0
          for _ in generator:
              counter += 1
          return counter
      else:
          return len(list(generator))
docs: Count number of item in generator.
  
      memory_efficient=True, 3 times slower, but memory_efficient.
      memory_efficient=False, faster, but cost more memory.