I have had success staying about 5nm/9km away from them.. Inside that distance it's a crap shoot and appears to depend on the quality the gunners the AI assigned to the guns...
Obviously submerging to periscope depth solves the problem at the expense of speed, time and battery capacity..