My question is: Should I use uint40 for time in Solidity?

All the examples I've seen use uint256 for time, however, I think it's too wasteful -- while using uint32 is maybe a little aggressive due to its 86-year-till-overflow, the 34K-year-till-overflow from uint40 seems pretty safe to me.

Could there be any potential downfall from using uint40 that I'm not aware of?


Sorry for not making myself clear enough earlier. Consider the following struct --

struct User {
    address user;   // 20 bytes
    uint40 userRegisterTime;    // 5 bytes
    uint40 userDepositTime;     // 5 bytes
    uint256 userBalanceWei;     // 32 bytes

Had the two time vars been defined as uint256, the struct would need 4 * 32-byte, while in the case of uint40, the first three vars can be packed into one 32-byte, saving 2 * 32-byte (reference, which I have verified -- https://medium.com/@novablitz/storing-structs-is-costing-you-gas-774da988895e )

Everybody agrees?

  • 1
    Better wasteful than being at risk of getting an overflow in one of your calculations, as a result of the compiler choosing the shortest type which is sufficient for the operation. For example, time * 2 will be stored into uint40, while the result might be larger than 40 bits. For this purpose only, it is already better to just keep all your stuff in uint256s. As to gas cost, keeping all your data aligned to 256 bits, will actually yield lower gas-cost. Jun 2, 2019 at 9:54
  • @goodvibration do you have a citation to back the claim that keeping all data as 256-bit yields lower gas costs? I'd love to learn more why.
    – Greg
    Nov 1, 2022 at 10:37
  • @goodvibration's objection no longer applies in Solidity v0.8, since the compiler checks arithmetic overflows by default now. Nov 13, 2022 at 16:10

2 Answers 2


Solidity's VM is 256 bit, so uint40 will be padded to 256 bits and take more gas

Why does uint8 cost more gas than uint256?

  • 1
    For function variables, yes; For storage variables, smaller types could be more gas efficient -- I've added a sample case to my question -- sorry for missing it out earlier.
    – Jerry Ji
    Jun 2, 2019 at 11:18
  • For this exact case - yes. Storing the data will take less space, but it depends on your calculations inside the contract if it will take less or more gas overall, so it's a balance choice on your own.
    – KNK
    Jun 2, 2019 at 14:44
  • Also this is not true is using a lot of variables in the same contract. They will be padded together in small memory footprint.
    – imekinox
    Dec 10, 2021 at 0:25

I have been thinking the same. In my case it is valid, and in your case, too.

If people in 34K years still use the same technology as we do, they are doomed anyway. :-D So it actually is a feature, not a bug when your software stops working then.

  • This answer consists mostly of personal opinions. Check question comments and other answers to learn that, apparently, using uint40 can yield higher gas costs. (e.g. ethereum.stackexchange.com/a/71367/31994)
    – Greg
    Nov 1, 2022 at 10:38
  • Sorry, I have a different opinion. First, the author was elaborating on years and asks for an opinion. Second, he is using the uin40 in a struct, so packing the bytes makes sense. Nov 11, 2022 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.