(setq hasMilliSecs (/= (getvar "MILLISECS") nil))
(setvar "LOGFILEMODE" 0)
(vl-load-com)

(if (and (not vle-vector-getperpvector) (findfile "vle-extension.lsp")) (load "vle-extension.lsp"))

(defun emul-vector-getperpvector ( vec / vX vY vZ len res )
  (if (/= (type vec) 'LIST) (exit))

  (setq vX (car vec) vY (cadr vec))
  (if (or (not vX) (not vY)) (exit))

  (setq vZ (caddr vec))
  (if (not vZ) (setq vZ 0.0))

  (if (and (< (abs vX) vle_g_vecTol) (< (abs vY) vle_g_vecTol))
    (if (< (abs vZ) vle_g_vecTol)
      (setq res (list (* -1.0 vY) vX 0.0))
      (setq res (list vZ 0.0 0.0))
    )
    (setq res (list (* -1.0 vY) vX 0.0))
  )

  (setq vX (car res) vY (cadr res) vZ (caddr res))

  (setq len (sqrt (+ (* vX vX) (* vY vY) (* vZ vZ))))
  (if (equal len 0.0 vle_g_vecTol) (setq len 1.0))
  (setq len (/ 1.0 len))

  (list (* vX len) (* vY len) (* vZ len))
)

(defun t_vle_vector_getperpvector ( count fptr / v2d v3d time1 time2 time3 time4 )

  ;;--- build data list
  (setq v2d '(22.0 33.0) v3d '(1 2 3))

  ;;--- now run test : NORMAL
  (setq time1 (getvar "MILLISECS"))
  (if (not time1) (setq time1 (getvar "TDUSRTIMER")))
  (if (not time1) (setq time1 0.0))

  (repeat (* count count)
    (emul-vector-getperpvector v2d)
    (emul-vector-getperpvector v3d)
  )

  (setq time2 (getvar "MILLISECS"))
  (if (not time2) (setq time2 (getvar "TDUSRTIMER")))
  (if (not time2) (setq time2 0.0))

  ;;--- now run test : VLE
  (setq time3 (getvar "MILLISECS"))
  (if (not time3) (setq time3 (getvar "TDUSRTIMER")))
  (if (not time3) (setq time3 0.0))

  (repeat (* count count)
    (vle-vector-getperpvector v2d)
    (vle-vector-getperpvector v3d)
  )

  (setq time4 (getvar "MILLISECS"))
  (if (not time4) (setq time4 (getvar "TDUSRTIMER")))
  (if (not time4) (setq time4 0.0))

  ;;--- test results
  (setq bench$secsNorm (if hasMilliSecs (* 0.001 (- time2 time1)) (* 86400.0 (- time2 time1))))
  (setq bench$secsFast (if hasMilliSecs (* 0.001 (- time4 time3)) (* 86400.0 (- time4 time3))))
  (setq bench$seconds (+ bench$secsNorm bench$secsFast))

  (princ (strcat "* (Standard) : " (rtos bench$secsNorm 2 3) "  VLE-VECTOR-GETPERPVECTOR : " (rtos bench$secsFast 2 3) "\n"))
  (if fptr
    (princ (strcat "* (Standard) : " (rtos bench$secsNorm 2 3) "  VLE-VECTOR-GETPERPVECTOR : " (rtos bench$secsFast 2 3) "\n") fptr)
  )

  bench$seconds
)

;; to increase/decrease the number of bench loops : > 1 increase < 1 decrease
(if (not $benchcyclesfactor) (setq $benchcyclesfactor 1))

;; to run this loaded test manually
(defun C:Run ()
  (setvar "cmdecho" 0)
  (print)
;  (t_vle_vector_negate (* 400 $benchcyclesfactor) nil)
(t_vle_vector_getperpvector (* 1000 $benchcyclesfactor) nil)
  (setvar "cmdecho" 1)
  (princ)
)
