Because it needs to establish a TCP connection with each endpoint and I guess has a default timeout time. So every failing connection waits for the timeout to pass.
If you want to write a port scanner, you're probably better off using existing solutions or using sockets directly, so you can adjust the behavior to your needs.