Skip to content

Commit 82b7dee

Browse files
Lakshmandhschall
authored andcommitted
Upgraded fibonacci to deal with large numbers.
Signed-off-by: L Lakshmanan <[email protected]>
1 parent b1b9aa7 commit 82b7dee

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

benchmarks/fibonacci/go/server.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"fmt"
3030
"net"
3131
"strconv"
32+
"math/big"
3233

3334
pb "github.com/vhive-serverless/vSwarm-proto/proto/fibonacci"
3435
tracing "github.com/vhive-serverless/vSwarm/utils/tracing/go"
@@ -42,14 +43,19 @@ var (
4243
address = flag.String("addr", "0.0.0.0:50051", "Address:Port the grpc server is listening to")
4344
)
4445

45-
func fibonacci(num int) float64 {
46-
var num1 float64 = 0
47-
var num2 float64 = 1
48-
var sum float64
46+
func fibonacci(num int) *big.Int {
47+
if num <= 0 {
48+
return big.NewInt(0)
49+
}
50+
51+
num1 := big.NewInt(0)
52+
num2 := big.NewInt(1)
53+
sum := big.NewInt(0)
54+
4955
for i := 0; i < num; i++ {
50-
sum = num1 + num2
51-
num1 = num2
52-
num2 = sum
56+
sum.Add(num1, num2)
57+
num1.Set(num2)
58+
num2.Set(sum)
5359
}
5460
return num1
5561
}
@@ -64,7 +70,7 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
6470
// log.Printf("Received: %v", in.GetName())
6571
x, _ := strconv.ParseInt(in.GetName(), 10, 64)
6672
var y = fibonacci(int(x))
67-
resp := fmt.Sprintf("fn: Fib: y = fib(x) | x: %d y: %.1f | runtime: GoLang", x, y)
73+
resp := fmt.Sprintf("fn: Fib: y = fib(x) | x: %d y: %s | runtime: GoLang", x, y.String())
6874
return &pb.HelloReply{Message: resp}, nil
6975
}
7076

benchmarks/fibonacci/nodejs/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ var hello_proto = grpc.loadPackageDefinition(packageDefinition).fibonacci;
4141

4242

4343
function fibonacci(num) {
44-
var num1 = 0;
45-
var num2 = 1;
46-
var sum;
44+
let num1 = BigInt(0);
45+
let num2 = BigInt(1);
46+
let sum = BigInt(0);
4747
var i = 0;
4848
for (i = 0; i < num; i++) {
4949
sum = num1 + num2;
5050
num1 = num2;
5151
num2 = sum;
5252
}
53-
return num1;
53+
return num1.toString();
5454
}
5555

5656
/**

benchmarks/fibonacci/python/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
tracing.grpcInstrumentClient()
5959
tracing.grpcInstrumentServer()
6060

61+
sys.set_int_max_str_digits(500000)
62+
6163
def fibonacci(num):
6264
num1=0
6365
num2=1
@@ -79,7 +81,7 @@ def SayHello(self, request, context):
7981
y = fibonacci(x)
8082

8183
gid = syscall(104)
82-
msg = "fn: Fib: y = fib(x) | x: %i y: %.1f | runtime: python" % (x,y)
84+
msg = "fn: Fib: y = fib(x) | x: %i y: %i | runtime: python" % (x,y)
8385
return fibonacci_pb2.HelloReply(message=msg)
8486

8587

0 commit comments

Comments
 (0)