From 82b7dee46fca5688db3c6a1ce07584731f6a63a5 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Sun, 28 Jul 2024 00:05:27 -0600 Subject: [PATCH] Upgraded fibonacci to deal with large numbers. Signed-off-by: L Lakshmanan --- benchmarks/fibonacci/go/server.go | 22 ++++++++++++++-------- benchmarks/fibonacci/nodejs/server.js | 8 ++++---- benchmarks/fibonacci/python/server.py | 4 +++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/benchmarks/fibonacci/go/server.go b/benchmarks/fibonacci/go/server.go index e1b318fe6..f5d277d54 100644 --- a/benchmarks/fibonacci/go/server.go +++ b/benchmarks/fibonacci/go/server.go @@ -29,6 +29,7 @@ import ( "fmt" "net" "strconv" + "math/big" pb "github.com/vhive-serverless/vSwarm-proto/proto/fibonacci" tracing "github.com/vhive-serverless/vSwarm/utils/tracing/go" @@ -42,14 +43,19 @@ var ( address = flag.String("addr", "0.0.0.0:50051", "Address:Port the grpc server is listening to") ) -func fibonacci(num int) float64 { - var num1 float64 = 0 - var num2 float64 = 1 - var sum float64 +func fibonacci(num int) *big.Int { + if num <= 0 { + return big.NewInt(0) + } + + num1 := big.NewInt(0) + num2 := big.NewInt(1) + sum := big.NewInt(0) + for i := 0; i < num; i++ { - sum = num1 + num2 - num1 = num2 - num2 = sum + sum.Add(num1, num2) + num1.Set(num2) + num2.Set(sum) } return num1 } @@ -64,7 +70,7 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe // log.Printf("Received: %v", in.GetName()) x, _ := strconv.ParseInt(in.GetName(), 10, 64) var y = fibonacci(int(x)) - resp := fmt.Sprintf("fn: Fib: y = fib(x) | x: %d y: %.1f | runtime: GoLang", x, y) + resp := fmt.Sprintf("fn: Fib: y = fib(x) | x: %d y: %s | runtime: GoLang", x, y.String()) return &pb.HelloReply{Message: resp}, nil } diff --git a/benchmarks/fibonacci/nodejs/server.js b/benchmarks/fibonacci/nodejs/server.js index 69f311c3a..3921ec54b 100644 --- a/benchmarks/fibonacci/nodejs/server.js +++ b/benchmarks/fibonacci/nodejs/server.js @@ -41,16 +41,16 @@ var hello_proto = grpc.loadPackageDefinition(packageDefinition).fibonacci; function fibonacci(num) { - var num1 = 0; - var num2 = 1; - var sum; + let num1 = BigInt(0); + let num2 = BigInt(1); + let sum = BigInt(0); var i = 0; for (i = 0; i < num; i++) { sum = num1 + num2; num1 = num2; num2 = sum; } - return num1; + return num1.toString(); } /** diff --git a/benchmarks/fibonacci/python/server.py b/benchmarks/fibonacci/python/server.py index dc73a2502..d91ea3037 100644 --- a/benchmarks/fibonacci/python/server.py +++ b/benchmarks/fibonacci/python/server.py @@ -58,6 +58,8 @@ tracing.grpcInstrumentClient() tracing.grpcInstrumentServer() +sys.set_int_max_str_digits(500000) + def fibonacci(num): num1=0 num2=1 @@ -79,7 +81,7 @@ def SayHello(self, request, context): y = fibonacci(x) gid = syscall(104) - msg = "fn: Fib: y = fib(x) | x: %i y: %.1f | runtime: python" % (x,y) + msg = "fn: Fib: y = fib(x) | x: %i y: %i | runtime: python" % (x,y) return fibonacci_pb2.HelloReply(message=msg)