Skip to main content
added 17 characters in body
Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

C++, 140 µs

This is a trivial answer that uses GMP, but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

#include <chrono> #include <gmpxx.h> #include <iostream> #include <vector> int main() { std::vector<mpz_class> numbers; mpz_class number; while (std::cin >> number) numbers.push_back(number); auto t0 = std::chrono::high_resolution_clock::now(); for (auto &number : numbers) number = sqrt(number); auto t1 = std::chrono::high_resolution_clock::now(); for (const auto number&number : numbers) std::cout << number << '\n'; std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ns\n"; } 

Try it online!Try it online!

C++, 140 µs

This is a trivial answer that uses GMP, but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

#include <chrono> #include <gmpxx.h> #include <iostream> #include <vector> int main() { std::vector<mpz_class> numbers; mpz_class number; while (std::cin >> number) numbers.push_back(number); auto t0 = std::chrono::high_resolution_clock::now(); for (auto &number : numbers) number = sqrt(number); auto t1 = std::chrono::high_resolution_clock::now(); for (auto number : numbers) std::cout << number << '\n'; std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ns\n"; } 

Try it online!

C++, 140 µs

This is a trivial answer that uses GMP, but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

#include <chrono> #include <gmpxx.h> #include <iostream> #include <vector> int main() { std::vector<mpz_class> numbers; mpz_class number; while (std::cin >> number) numbers.push_back(number); auto t0 = std::chrono::high_resolution_clock::now(); for (auto &number : numbers) number = sqrt(number); auto t1 = std::chrono::high_resolution_clock::now(); for (const auto &number : numbers) std::cout << number << '\n'; std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ns\n"; } 

Try it online!

added 34466 characters in body
Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

HaskellC++, 800140 µs

This is a trivial answer that uses existing libraries (integer-roots, with deepseq and time for benchmarking)GMP, but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

import Control.DeepSeq import Data.Time.Clock import Math.NumberTheory.Roots main :: IO () main = do contents <- getContents numbers <- return $!! map read (lines contents) :: IO [Integer] t0 <- getCurrentTime sqrts <- return $!! map integerSquareRoot numbers t1 <- getCurrentTime putStr (unlines (map show sqrts)) print (diffUTCTime t1 t0) 
#include <chrono> #include <gmpxx.h> #include <iostream> #include <vector> int main() { std::vector<mpz_class> numbers; mpz_class number; while (std::cin >> number) numbers.push_back(number); auto t0 = std::chrono::high_resolution_clock::now(); for (auto &number : numbers) number = sqrt(number); auto t1 = std::chrono::high_resolution_clock::now(); for (auto number : numbers) std::cout << number << '\n'; std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ns\n"; } 

Try it online!

Haskell, 800 µs

This is a trivial answer that uses existing libraries (integer-roots, with deepseq and time for benchmarking), but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

import Control.DeepSeq import Data.Time.Clock import Math.NumberTheory.Roots main :: IO () main = do contents <- getContents numbers <- return $!! map read (lines contents) :: IO [Integer] t0 <- getCurrentTime sqrts <- return $!! map integerSquareRoot numbers t1 <- getCurrentTime putStr (unlines (map show sqrts)) print (diffUTCTime t1 t0) 

C++, 140 µs

This is a trivial answer that uses GMP, but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

#include <chrono> #include <gmpxx.h> #include <iostream> #include <vector> int main() { std::vector<mpz_class> numbers; mpz_class number; while (std::cin >> number) numbers.push_back(number); auto t0 = std::chrono::high_resolution_clock::now(); for (auto &number : numbers) number = sqrt(number); auto t1 = std::chrono::high_resolution_clock::now(); for (auto number : numbers) std::cout << number << '\n'; std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count() << "ns\n"; } 

Try it online!

Source Link
Anders Kaseorg
  • 40.7k
  • 3
  • 76
  • 149

Haskell, 800 µs

This is a trivial answer that uses existing libraries (integer-roots, with deepseq and time for benchmarking), but I’m submitting it anyway to drive home the point that the test cases are way too small. Scores in the range of literally nanoseconds per input cannot meaningfully be compared. We need larger test cases for this to be an interesting challenge.

import Control.DeepSeq import Data.Time.Clock import Math.NumberTheory.Roots main :: IO () main = do contents <- getContents numbers <- return $!! map read (lines contents) :: IO [Integer] t0 <- getCurrentTime sqrts <- return $!! map integerSquareRoot numbers t1 <- getCurrentTime putStr (unlines (map show sqrts)) print (diffUTCTime t1 t0)