Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-instant-1· AnthropicHugging Face Arena Human Preference 55K

Optimize the following code. def polllro( self, operations: List[Operation]) -> None:…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-pro’s answer.
  1. User

    Optimize the following code.

    def _poll_lro( self, operations: List[Operation]) -> None: """ Polls the long running operations. """ start_time = time.time()

        while True:
            status = [exponential_backoff(op.done) for op in operations]
    
            if all(status):
                print("Completed!")
                break
    
            print(f"{sum(status)}/{len(status)} completed")
            time.sleep(60)
    
        end_time = time.time()
    
        print(f"Time taken: {end_time-start_time} secs")
    
  2. Claude Assistant

    Here is one way to optimize the code:

    1. Use a generator expression instead of a list comprehension to avoid creating an intermediate list:
    status = (exponential_backoff(op.done) for op in operations)
    
    1. Check for completion with any() instead of all() to short-circuit as soon as one completes:
    if any(status):
    
    1. Print status only occasionally to avoid unnecessary I/O:
    print_counter = 0
    for status in status:
      print_counter += 1
      if print_counter % 10 == 0:
        print(...)
    
    1. Use more descriptive variable names than op, status etc.

    2. Add timeout in case operations get stuck

    Here is the optimized code:

    from time import time
    
    def poll_lro(operations):
    
      start = time()
    
      print_counter = 0
      timeout = 3600 
    
      while time() - start < timeout:
    
        statuses = (is_done(operation) for operation in operations)
    
        if any(statuses):
          print("Completed!")
          break
    
        print_counter += 1
        if print_counter % 10 == 0:
          done = sum(statuses)  
          total = len(operations)
          print(f"{done}/{total} completed")
    
        sleep(60)
    
      end = time()
    
      print(f"Time taken: {end-start} secs")
    

    This makes the code more readable and efficient by avoiding unnecessary list operations, short-circuiting on first completion, and printing status less frequently.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.