Last active
June 16, 2024 08:18
-
-
Save Lucodivo/175ca905505347f4e9095619b7ad846a to your computer and use it in GitHub Desktop.
Performance Test: Spacer vs. Padding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.packagename.spacervspadding | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.outlined.Refresh | |
import androidx.compose.material3.FloatingActionButton | |
import androidx.compose.material3.Icon | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
class MainActivity: ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
SpacerVsPadding() | |
} | |
} | |
} | |
const val HEIGHT = 45 | |
const val WIDTH = 70 | |
@Composable | |
fun SpacerVsPadding() { | |
var showContent by remember { mutableStateOf<Boolean?>(null) } | |
val paddingModifier = Modifier.padding(end = 1.dp) | |
val spacerModifier = Modifier.width(1.dp) | |
showContent?.let { padding -> | |
if(padding) { | |
val startNs = System.nanoTime() | |
Column { | |
repeat(HEIGHT) { | |
Row { | |
repeat(WIDTH) { | |
Text(text = ".", modifier = paddingModifier) | |
} | |
} | |
} | |
} | |
val paddingTimeNs = System.nanoTime() - startNs | |
Log.d("SpacerVsPadding", "Padding: Total - ${paddingTimeNs}ns") | |
} else { | |
val startNs = System.nanoTime() | |
Column { | |
repeat(HEIGHT) { | |
Row { | |
repeat(WIDTH) { | |
Text(text = ".") | |
Spacer(modifier = spacerModifier) | |
} | |
} | |
} | |
} | |
val spacerTimeNs = System.nanoTime() - startNs | |
Log.d("SpacerVsPadding", "Spacer: Total - ${spacerTimeNs}ns") | |
} | |
} | |
Box(modifier = Modifier.fillMaxSize()){ | |
FloatingActionButton( | |
onClick = { showContent = if(showContent == false) true else false }, | |
modifier = Modifier.align(Alignment.BottomEnd) | |
){ | |
Icon(imageVector = Icons.Outlined.Refresh, contentDescription = "Refresh Icon") | |
} | |
} | |
} | |
@Preview | |
@Composable | |
fun PreviewSpacerVsPadding() = SpacerVsPadding() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment