Skip to content

Commit

Permalink
Upgraded fibonacci to deal with large numbers.
Browse files Browse the repository at this point in the history
Signed-off-by: L Lakshmanan <[email protected]>
  • Loading branch information
Lakshman authored and Lakshman committed Jul 29, 2024
1 parent b1b9aa7 commit c0fc4fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
22 changes: 14 additions & 8 deletions benchmarks/fibonacci/go/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions benchmarks/fibonacci/nodejs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/fibonacci/python/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
tracing.grpcInstrumentClient()
tracing.grpcInstrumentServer()

sys.set_int_max_str_digits(500000)

def fibonacci(num):
num1=0
num2=1
Expand All @@ -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)


Expand Down

0 comments on commit c0fc4fa

Please sign in to comment.