It might be more efficient to try to create an edge between the two verts. If there already is one, an exception will be raised. Otherwise, an edge will be created - thus, you need to remove that edge again.
Thanks for the tip @CoDEmanX
I decided to do a performance test:
import bpy, bmesh, timeit
def test_edge1(V1, V2):
try:
edge = bm.edges.new([V1, V2])
except ValueError:
print('test_edge1: this edge exists')
def test_edge2(V1, V2):
if V2 in [x for y in [a.verts for a in V1.link_edges] for x in y if x != V1]:
print('test_edge2: this edge exists')
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
V1 = bm.edges[0].verts[0]
V2 = bm.edges[0].verts[1]
if __name__ == '__main__':
test1 = timeit.timeit("test_edge1(V1, V2)", setup="from __main__ import test_edge1, V1, V2", number=100)
test2 = timeit.timeit("test_edge2(V1, V2)", setup="from __main__ import test_edge2, V1, V2", number=100)
print('resultado:', test1/test2, 'diferenca:', test1-test2)
#bmesh.update_edit_mesh(me, True)
And the result is that really your solution presents a vanishingly performance a little better. It depends on a few factors in mesh.
But I will still continue with the first solution. Because you do not need to remove the edge.