https://leonardogerheim.com Leonardo Gerheim Fri, 06 Sep 2024 17:29:58 +0000 pt-PT hourly 1 How to save money in cloud computing – memoization https://leonardogerheim.com/2024/09/06/how-to-save-money-in-cloud-computing-memoization/ https://leonardogerheim.com/2024/09/06/how-to-save-money-in-cloud-computing-memoization/#respond Fri, 06 Sep 2024 17:29:58 +0000 https://leonardogerheim.com/?p=96

Following the serie about how to reduce costs in cloud computing, at this post we’re going to present another technique which should be very helpful.

The main ideia behind the walls is to keep tracking of the previous calculation to do not need to recalculate each time again.

The most famous example about memoization is its application in Fibonacci calculation. The Fibonacci number sequence is found in many nature situations like, shells, galaxies and so on.

In case your not so familiar with the sequence, to find the value of a specific position of the array you must sum the two previous values.

Below follow an implementation of the the algorithm

  function fibonacci(target: number): number {
    if (target < 2) return 1;

    return fibonacci(target - 1) + fibonacci(target - 2);
  }

The implementation is very simple even in a recursive way:

  • Validation of the base case, in case value is lower than 2
  • Call the function recursively adding the two previous values of the target

When you start to do some tests trying to find the 11th Fibonacci number you gonna be ok, but when reach out the 40th element boundary you gonna realize that running time start increase a lot. Of course it depends on your personal computer.

The chart above presents the exponential growth of the function. But there is a clever way to reduce that. But before, lets explained what is the problem. Calling recursively to find the 5th element you must find the 4th and the 3rd, to find 3rd must find the 2nd and the first. Take a look at tree below.

You gonna realize that to find out the 5th element, you calculate the 2nd 3 times. Imagine it when you try to find the 100th element…

The optimized solution

In fact the solution is simple and elegant, as everything in computing should be. We must create an structure to store the previous calculation always look for the value on this structure before calculate again. Lets take a look at the code below.

  function fibonacciMemo(
    target: number,
    memo: Record<number, number> = {}
  ): number {
    if (target < 2) return 1;

    if (memo[target]) {
      return memo[target];
    } else {
      memo[target] =
        fibonacciMemo(target - 1, memo) + 
        fibonacciMemo(target - 2, memo);
    }
    return memo[target];
  }

The code above the steps:

  • Initiate an empty object
  • define our base case to stop: Target lower than 2
  • IF target is found in memo object, return it
  • ELSE store it on memo
  • Reaching out base case, return target value from memo

Memoization is not an algorithm by its own, but a technique which can support many algorithms reducing its running time.

Conclusion

In this case, using memoization, until Fibonacci of 5000 you are under ONE second of running time, it is tremendous.

You need to remember that running time is money in cloud computing, so never forget to use some kind of approach reduce the time and consequently reduce costs with cloud computing.

Stay tuned to next posts.

]]>
https://leonardogerheim.com/2024/09/06/how-to-save-money-in-cloud-computing-memoization/feed/ 0
How to save money in cloud computing – a practical application https://leonardogerheim.com/2024/08/29/how-to-save-money-in-cloud-computing-practical-application/ https://leonardogerheim.com/2024/08/29/how-to-save-money-in-cloud-computing-practical-application/#respond Thu, 29 Aug 2024 21:41:00 +0000 https://leonardogerheim.com/?p=72

On the past posts we’ve introduced and explained some aspects about the evolution of computing services from on premises to cloud. The also quoted that time complexity is one of the keys to reduce costs in cloud computing.

Today we’re going to give you an example of how to implement a code improvement to reduce the running time of a function and reduce costs in cloud computing.

This example was written in Typescript, but the very important thing about it is the concept under the hood.

The problem:

Imagine a situation where you have a non ordered list of numbers and a target. The goal is to find out a pair of number on this list which is equal to the target value.

The output should be [0, 2] where it represents the indexes of the first two elements of the input list. Another thing is that there is just one answer.

One of the possible ways to solve this problem, maybe of the most intuitive could be create an index which will start in the begging of the input list and another one starting from the end of the list, going to middle of the list.

In each movement you should analise either the sum is equal or not to target value and decide increment the start index or decrement the end index. I think the code is very easy to understand.

The code below is the implementation of this algorithm.

function targetSum(nums: number[], target: number): number[] {
  let pStart: number = 0;
  let pEnd: number = nums.length - 1;
  const found: boolean = false;

  while (!found) {
    if (nums[pStart] + nums[pEnd] === target) {
      break;
    }

    if (nums[pEnd - 1] + nums[pStart] < nums[pStart + 1] + nums[pEnd]) {
      pEnd--;
    } else if (nums[pEnd - 1] + nums[pStart] === target) {
      pEnd--;
      break;
    } else if (nums[pStart + 1] + nums[pEnd] === target) {
      pStart++;
      break;
    } else {
      pStart++;
    }
  }
  const result = [pStart, pEnd];
  return result;
}

The code above run perfectly well, you’ll not receive any error from the compiler and so on, but… a capital BUT we’re focusing on reduce the running time, and this approach could be improved to reduce costs after all: TIME IS MONEY

The optimized solution

A best approach could be go in one direction from the begging to the end and calculate the differences between the current value and the target; after that store in a hashmap the current value of the input and its position. At each iteration, after calculate the difference, we look for the difference value on the hashmap. Take a look at the diagram below

The implementation of the discussed code is very simple as you can see below:

  function targetSumOpt(input: number[], target: number): number[] {
    const result = new Map();

    for (let index = 0; index < input.length; index++) {
      const difference = target - input[index];

      result.set(input[index], index);

      if (result.has(difference)) {
        return [result.get(difference), index];
      }
    }

    return [];
  }

When we are talking about reduce cloud cost through code optimization there is no a cake recipe or silver bullet technique we must analise each case. There is a bunch of resources on computing’s bat belt and we’ll explain some of them in the next posts.

Hope you’re finding helpful this series.

]]>
https://leonardogerheim.com/2024/08/29/how-to-save-money-in-cloud-computing-practical-application/feed/ 0
How to save money in cloud computing being a software engineer – part I https://leonardogerheim.com/2024/08/23/how-to-save-money-in-cloud-computing-being-a-software-engineer-part-i/ https://leonardogerheim.com/2024/08/23/how-to-save-money-in-cloud-computing-being-a-software-engineer-part-i/#respond Fri, 23 Aug 2024 17:46:06 +0000 https://leonardogerheim.com/?p=68 On the last post we did an introduction to the subject, and as promised now we’ll go deeper into the subject

To save money on cloud, depends on many factors and which cloud service you are using and aiming to save money. For database service you must considering IOPS, storage, data transfer and so on…

Otherwise in almost every cloud service, a factor which has a direct impact on your cloud billing is: PROCESSING TIME

Because of that, we’ll focus on this specific cost key to get improved.

Time complexity

Backing to algorithm classes we’ve learned about time complexity

It means that we must run away from green line O(n!) and do everything what is possible to get close to the black line which means constant time.

Summarizing the graph bellow how much we are close to the left side the input data do a big impact in the final time to process. In the other words, in case we have an array of 1.000 positions, with a O(n!) algorithm each time you increase your input you will have a factorial increment in you time complexity against NONE increment using a O(1) algorithm.

Take a look on the image bellow taken from the book Grokking Algorithms from
Aditya Bhargava.

Here we can see, considering some famous algorithms ant their time complexities through out different payload.

If you didn’t make the connection yet i’m gonna type it in capital letters:

TIME COMPLEXITY AND SAVE MONEY IN CLOUD SOLUTIONS ARE CORRELATED

In the cloud age engineers are not so compromised to optimize the code because we can easily use auto scaling, just increase the memory or even cpu. Optimization is in many cases very complicated to achieve and a concern from senior engineers and big companies. Because you must understand that there is no free lunch, there is always a price you got to pay.

Just let you think about, to train the GPT-4 version was spent around $100 Millions dollars.

Next post we’re going to give you a real world code example of how to reduce time complexity in production and save money.

]]>
https://leonardogerheim.com/2024/08/23/how-to-save-money-in-cloud-computing-being-a-software-engineer-part-i/feed/ 0
How to save money in cloud computing being a software engineer – intro https://leonardogerheim.com/2024/08/13/how-to-save-money-in-cloud-computing-being-a-software-engineer-intro/ https://leonardogerheim.com/2024/08/13/how-to-save-money-in-cloud-computing-being-a-software-engineer-intro/#respond Tue, 13 Aug 2024 18:06:02 +0000 https://leonardogerheim.com/?p=53 I decided to write a post about how to save money in cloud computing. The post started to be very long and I change my mind to split it in a serie of posts on this theme. I’ll guide you through some concepts definitions about computing and cloud until we reach out the root cause of costs in cloud computing: software engineering.

So in case you are a generation Z member, probably you don’t know anything about on premise computing, routing tables, buy a new server and so on… You have born in the cloud days, you have never compiled a Linux kernel in your life, you truly believe that your macbook runs everything in some magic way, just needing to access the right website.

For me, a member of the Millennium generation, the things are completely different. I have seen all the migration from buy a new server, install the Linux o.s, configure all the things and share resources through the intranet network.

The first main difference between the old way for the cloud model is that: before the cloud age you planned which resources you needed, presented the budget to the I.T manager and got the approval to buy a brand new server. After four months you realized that you need one more server, because the number of user increased. In a very perfect path, you needed to ask for a new server for your manager, he approve on the same day and a month before you got the sever, spent two days configuring all the thing and voilà…

Considering that, you needed to request for resources very carefully, because in case you need some more, the time the between the request for a resource and its productization could be very long.

Nowadays the things are very different, in a mouse’s click you can increase your infrastructure in some minutes or even seconds. Now we have a powerful mechanism to literally send our budget through the infinity and beyond.

In many situations we have this two faces of the same coin: on the one hand the ability to downtime the productization resource, on other hand the ability to guide to ruin the project budget.

Using another analogy:

In the cloud approach we have control over the tap, but not over the water meter.

The issue remains:

How to take control of the cloud costs? And most important of all, how to save money in cloud deployed resources?

Should everything move to cloud?

The promise of cloud was: Cloud is cheaper than have your own infrastructure on your own physic space. After almost two decades of cloud, it is not so easy like that, it depends on a bunch of factors which we must consider.

Imagining a very specific scenario: We have a 150-200 employees company which everyone has access to our internal HR web platform using a SQL database. In this not so hypothetical scenario, try to answer:

is better to move to cloud or keep that in your own server?

AGAIN, it is not a straight answer, we must to consider a bunch of factors, which we’ll not approach at this post. We’re going to focus on the engineering side, because software engineering is one of the key features for save money in cloud or even outside its.

Next post we’re going enter in details about how to save money in cloud being a software engineer.

]]>
https://leonardogerheim.com/2024/08/13/how-to-save-money-in-cloud-computing-being-a-software-engineer-intro/feed/ 0
To know a lot about nothing https://leonardogerheim.com/2024/06/23/to-know-a-lot-about-nothing/ https://leonardogerheim.com/2024/06/23/to-know-a-lot-about-nothing/#respond Sun, 23 Jun 2024 03:59:04 +0000 https://leonardogerheim.com/?p=43 Some days ago I watched a very interesting video from mr. Richard Feynman. He was an American physicist who won a Nobel price in 1965. I know nothing about his work, but know some things about him. He had television show where He taught physics. He helped Crack the Case on the Challenger Disaster, which was a tragic and same time famous space aircraft accident in 1986.

In case you do not remember him with the last paragraph informations, maybe I can try to give you some more interesting facts about his life. He was one of scientists who worked on the Manhattan project which the ultimate goal was to provide a nuclear bomb. Just in case you have watched the Oppenheimer movie, mr. Feynman was the man who during the bomb test scene didn’t wear glasses, just taking shelter on the truck.

The video which I mentioned to have watched, mr. Feynman remembered an episode about his childhood when his daddy showed him a bird and started to present to little Richard the name of that bird in some different languages from English, like Portuguese, Italian, Chinese and Japanese. After that his father argued:

“You can know the name of a bird in all the languages of the world, but when you’re finished, you’ll know absolutely nothing whatever about the bird… So let’s look at the bird and see what it’s doing — that’s what counts.”

Such a great lesson We have here… For many times and situations in our lives we can do the same, just knowing the name of a bird in a different language, specially in the social medias.

Mr. Feynman also said:

“I learned very early the difference between knowing the name of something and knowing something.”

That is a very important decision, do not keep ourselves in the shallow of something, but go further and deep. Never trust in any internet discourse from any Guru just saying a lot about nothing.

See you around

]]>
https://leonardogerheim.com/2024/06/23/to-know-a-lot-about-nothing/feed/ 0
What you can find here https://leonardogerheim.com/2024/06/12/hello-world/ https://leonardogerheim.com/2024/06/12/hello-world/#comments Wed, 12 Jun 2024 14:04:16 +0000 https://leonardogerheim.com/?p=1 Hello folks, I’m very late about start a blog, have I missed the goal? I don’t know… Maybe nowadays a blog is a very cringe think, who knows… I don’t know…
I took a while to start, but believe me, It was not lack of desire, it was about my impostor syndrome…

During my life journey for many times I thought that I was not very good in something even when a smart person said that I was quite good in something. I always thought that either that person was just being polite, or didn’t know sufficient compared with another one and for that I needed the validation of a smartest people than me and that one who was praising me

After all I realize I was being cruel with myself, because every single day you can find someone better than you, someone more smart, beautiful, fast, rich and etc… I had two options, find a excuse or a motivation, I’ll try to find a motivation. Last week I watched a video and read a quote:

God gives some sort of cards to you play in life… some people receives a better hand them other. A bad hand do not define a game, it’s up to you be a good player

anonymous

I truly believe in that. I’m sure that I’m not the person who have started which a good sort of cards, but I’m very engaged to be a good player…
And along the way share with you some ideas just ion case it can help you.

So I like study different things in life like: theology, epistemology, computing, mathematics and Philosophy and that is main kind of articles that you can find here.

See you around

]]>
https://leonardogerheim.com/2024/06/12/hello-world/feed/ 1